#T#经常上网收集一些资料,古人云:一分耕耘,一分收获,这里就给大家讲讲关于ADO.NET _RecordsetPtr接口。希望大家有收获。_RecordsetPtr是一个记录集对象。与以上两种对象相比,它对记录集提供了更多的控制功能,如记录锁定,游标控制等。同_CommandPtr 接口一样,它不一定要使用一个已经创建的数据连接,可以用一个连接串代替连接指针赋给_RecordsetPtr的connection成员变量,让它自己创建数据连接。如果你要使用多个记录集,***的方法是同Command对象一样使用已经创建了数据连接的全局ADO.NET _RecordsetPtr接口,然后使用_RecordsetPtr执行存储过程和SQL语句。使用ADO.NET _RecordsetPtr接口(以连接SQL Server为例):
- _RecordsetPtr pPtr;
- if (FAILED(pPtr.CreateInstance("ADODB.Recordset")))
- {
- AfxMessageBox("Create Instance failed!");
- return FALSE;
- }
- CString strSRC;
- strSRC="Driver=SQL Server;Server=";
- strSRC+="210.46.141.145";
- strSRC+=";Database=";
- strSRC+="mydb";
- strSRC+=";UID=sa;PWD=";
- strSRC+="sa";
- CString strSQL = "select id,name,gender,address from personal";
- _variant_t varSRC(strSRC);
- _variant_t varSQL(strSQL);
- if(FAILED(pPtr->Open(varSQL,varSRC,adOpenStatic,adLockOptimistic,adCmdText)))
- {
- AfxMessageBox("Open table failed!");
- pPtr.Release();
- return FALSE;
- }
- while(!pPtr->GetadoEOF())
- {
- _variant_t varNo;
- _variant_t varName;
- _variant_t varSex;
- _variant_t varAddress;
- varNo = pPtr->GetCollect ("id");
- varName = pPtr->GetCollect ("name");
- varSex = pPtr->GetCollect ("gender");
- varAddress = pPtr->GetCollect ("address");
- CString strNo =(char *)_bstr_t(varNo);
- CString strName =(char *)_bstr_t(varName);
- CString strSex =(char *)_bstr_t(varSex);
- CString strAddress =(char *)_bstr_t(varAddress);
- strNo.TrimRight();
- strName.TrimRight();
- strSex.TrimRight();
- strAddress.TrimRight();
- int nCount = m_list.GetItemCount();
- int nItem = m_list.InsertItem (nCount,_T(""));
- m_list.SetItemText (nItem,0,strNo);
- m_list.SetItemText (nItem,1,strName);
- m_list.SetItemText (nItem,2,strSex);
- m_list.SetItemText (nItem,3,strAddress);
- pPtr->MoveNext();
- }
- pPtr->Close();
- pPtr.Release();