ADO连接MS SQL Server数据库(pubs)步骤
1:ADO继承OLE DB,而OLE DB基于com技术编写的.所以ADO本身也是COM组件
2:建立一个基本对话框
在stdafx.h
//导入动态链接库,改变结尾标识为rsEOF
#import "D:/Program files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","rsEOF")
2:界面中拖一个列表框控件,命名为IDC_LIST1
在查询按钮中增加如一代码
//初始化
CoInitialize(NULL);
//定义
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
//连字串
pConn->ConnectionString ="Provider=SQLOLEDB.1;Password=123456q;Persist SecurityInfo=True;User ID=sa;Initial Catalog=pubs";
//打开(同步)
pConn->Open ("","","",adConnectUnspecified);
//执行
pRst=pConn->Execute ("select * from authors",NULL,adCmdText);
while(!pRst->rsEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect ("au_lname"));
pRst->MoveNext (); //下标下移
}
pRst->Close (); //关闭记录集
pConn->Close (); //关闭连接
pRst.Release (); //释放智能指针在com接口上的引用计数,即释放记录集和连接对象
pConn.Release ();
CoUninitialize(); //卸载com库