打印本文 打印本文 关闭窗口 关闭窗口
通过COM使用ADO
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2249  更新时间:2009/4/23 10:50:36  文章录入:mintao  责任编辑:mintao
p; HRESULT hr;
   hr=m_recordset->get_Fields(Fields);
   if(hr==S_OK)
   {
      hr=(*Fields)->get_Count(&Count);
   }
   return hr;
}
HRESULT ADOrecordset::GetFields(long index,String& sFieldList)
{
   FieldsPtr* Fields;
   FieldPtr* Field;
   HRESULT hr;
   WideString FieldName;
   long rec=0;
   hr=m_recordset->get_Fields(Fields);
   if(hr==S_OK)
   {
      hr=(*Fields)->get_Count(&rec);
      if(hr==S_OK)
      {
         for(int i=0;i<hr;i++)
         {
            hr=(*Fields)->get_Item(i,Field);
            if(hr==S_OK)
            {
               hr=(*Field)->get_Name(&FieldName);
               if(hr==S_OK)
               {
                  sFieldList+=(String)FieldName;
               }
            }
         }
      }
   }
   return hr;
}
HRESULT GetFieldType(FieldPtr pField,String& strMsg,long& nResult)
{
   file://Nowadays I don''''t use the function,which will be used according to needs in the future
   return S_OK;
}
HRESULT GetFieldType(String FieldName,String& strMsg,long& nResult)
{
   file://Nowadays I don''''t use the function,which will be used according to needs in the future
   return S_OK;
}
BOOL    ADOrecordset::get_BOF()
{
   BOOL bFlag=m_recordset->get_BOF();
   return bFlag;
}
BOOL    ADOrecordset::get_EOF()
{
   BOOL bFlag=m_recordset->get_EOF();
   return bFlag;
}
HRESULT ADOrecordset::Prev()
{
   HRESULT hr=m_recordset->MovePrevious();
   return hr;
}
HRESULT ADOrecordset::Last()
{
   HRESULT hr=m_recordset->MoveLast();
   return hr;
}
HRESULT ADOrecordset::Next()
{
   HRESULT hr=m_recordset->MoveNext();
   return hr;
}
HRESULT ADOrecordset::First()
{
   HRESULT hr=m_recordset->MoveFirst();
   return hr;
}
HRESULT ADOrecordset::CloseRecordset(void)
{
   HRESULT hr=m_recordset->Close();
   return hr;
}
写完这个类之后,以后在程序中就可以直接使用这个类来对ADO进行访问,写一个简单的测试代码:
假定我已在ODBC中新建一数据源:DSN=Test,Username=chenbin,Password=caiyao,那么如果想用ADO对该数据源进行访问的话,就可以用下面的代码:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
   ADOConnection m_Connection;
   ADOrecordset rst;
   if(m_Connection.Open(L"Test",L"chenbin",L"caiyao"))
   {
      if(rst.SetConnect(m_Connection.m_pConnection))
      {
         if(rst.Open("select * from family"))
         {
            if(rst.First()==S_OK)
            {
               while(!rst.get_EOF())
               {
                  Memo1->Lines->Add(rst.GetFieldValue("name").AsString());
                  rst.Next();
               }
            }
         }
      }
   }
}

 

 

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口