|
|
发表于 2009-5-16 17:41:00
|
显示全部楼层
Re:求助,ADO连接SQL数据库的问题?
这是我写的 成功连接了
1.导入ADO动态链接库,在stdafx.h中加入:
#import "C:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2.在InitInstance()中加入:
if (!AfxOleInit())
{
AfxMessageBox("初始化OLE DLL 失败 ");
return FALSE;
}
来初始化OLE/COM库环境。
3.OLE DB提供者实现对SQL Sever的标准安全连接串:
CString strConnect;
strConnect=_T(" rovider=sqloledb;Data Source=file;"
"Initial Catalog=file;"
"User ID=MyUserName;Password=Mypassword;");
strSQL=_T("Provider=sqloledb;Data Source=PC-200901101031;Initial Catalog=hackfile;Integrated Security=SSPI;User ID=bolong;Password=101110");
或strSQL=_T("driver={sql server};server=192.168.177.80;database=hackfile;uid=bolong;pwd=101110");
远程连接:
4.打开结果集
BOOL CInternetBarApp::OpenRecordSet(_RecordsetPtr &recPtr,CString &strSQL)
{
CInternetBarApp* pApp=(CInternetBarApp*)AfxGetApp();
m_pRecordset.CreateInstance(_uuidof(Recordset));
try
{
recPtr->Open(strSQL.AllocSysString(),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
CString strError;
strError.Format("警告:打开数据库表时发生异常。错误信息:%s",e.ErrorMessage());
AfxMessageBox(strError);
return FALSE;
}
return TRUE;
}
|
|