您的位置:首页 > 数据库

MFC中ADO数据库连接

2013-12-31 17:27 169 查看
这是按照孙鑫C++视频第二十讲编写的,但是还没有在VS2012中找到如何得到ConnectionString的方法,待解决,多样数据库的连接

void CAdoDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CoInitialize(NULL) ;//初始Com库

	_ConnectionPtr pConn(__uuidof(Connection)) ;//__uuidof()获取Connection全局唯一标示符
	_RecordsetPtr  pRst(__uuidof(Recordset)) ;
	_CommandPtr    pCmd(__uuidof(Command)) ;

	pConn->ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=pubs" ;//在此添加链接字符串的语句
	pConn->Open("" , "" , "" , adConnectUnspecified) ;//查阅MSDN了解相关参数

	pRst = pConn->Execute("select * from authors" , NULL , adCmdText) ;
	pRst->Open("select * from authors" , _variant_t((IDispatch *)pConn) ,
		adOpenDynamic , adLockOptimistic , adCmdText) ;
	pCmd->put_ActiveConnection(_variant_t((IDispatch *)pConn)) ;
	pCmd->CommandText = "select * from authors" ;
	while (!pRst->rsEOF)
	{
		((CListBox *)GetDlgItem(IDC_LIST1))->AddString(
			(_bstr_t)pRst->GetCollect("au_lname")) ;
		pRst->MoveNext() ;
	}

	pRst->Close() ;
	pConn->Close() ;
	pCmd->Release() ;
	pCmd->Release() ;
	pRst->Release() ;
	pConn->Release() ;

	CoUninitialize() ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: