VC6ADO连接SQL2005
By 闲鸟归来 , Posted in 死亡笔记
01 /*dbo.bank数据库内有customername、currentmoney两列*/
02
03 /*在对应头文件+入一句:*/
04 #import "c:Program FilesCommon FilesSystemadomsado15.dll" no_namespace rename("EOF", "adoEOF")
05
06 /*如下为具体ADO连接与查询语句执行*/
07 CoInitialize(NULL); // 初始化COM环境
08 _ConnectionPtr cnn(__uuidof(Connection)); // 建立Connection
09 _RecordsetPtr rst(__uuidof(Recordset));
10 _CommandPtr cmd(__uuidof(Command));
11 cnn->ConnectionString = (_bstr_t)loginConnStr;
12 // CString loginConnStr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=bank;Data Source=PCADMIN\SQL2005"; // 数据源
13 cnn->Open("", "", "", adConnectUnspecified); // 数据源已经准备好,前3参数为空
14 cmd->put_ActiveConnection(_variant_t((IDispatch*)cnn));
15 cmd->CommandText = "select * from bank";
16 rst = cmd->Execute(NULL, NULL, adCmdText);
17 while ( !rst->adoEOF )
18 {
19 ::AfxMessageBox((_bstr_t)rst->GetCollect("customername")); // 直接MsgBox出来~
20 rst->MoveNext();
21 }
22 rst->Close(); // 关闭
23 cnn->Close();
24 rst.Release(); // 释放
25 cnn.Release();
26 CoUninitialize();
补充:软件开发 , Vc ,