ADO访问技术:
(1)Connection对象:管理应用程序和数据库的通信。
(2)Command对象:用来处理重复执行的操作,或处理需要检查在存储过程调用中的输出或返回参数的值的查询。
(3)Recordset对象:用来获取数据。存放查询的结果,由数据的行(记录)和列(字段)组成。
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF","rsEOF")//导入dll
CoInitialize(NULL);//初始化Com环境
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
pConn->ConnectionString = "Provider = SQLOLEDB.1;Password = 123456;Persist Security Info= True;User ID = sa;Initial Catalog = test";
HRESULT hr; //返回是否连接成功
hr = pConn->Open("","","",adConnectUnspecified);
if(FAILED(hr)) //if(((HRESULT)(hr)) < 0) //判断连接是否成功
{
MessageBox( _T("Can't linked sql"));//代表打开数据库失败
}
pRst = pConn->Execute(_T("Select * from tset1"),NULL,adCmdText);
while(!pRst->rsEOF)
{
int n = pRst->GetCollect("1");
pRst->MoveNext();
}
pRst->Close();
pConn->Close();
pRst->Release();
pConn->Release();
CoUninitialize();