//获得数据库连接字符串
void CMFCSQLDlg::OnConnStrClick()
{
// TODO: Add your control notification handler code here
HRESULT hr; //返回结果变量
_ConnectionPtr m_pConnection;
// 在应用程序的InitInstance函数里加入
CoInitialize(NULL) ;
hr=m_pConnection.CreateInstance("ADODB.Connection"); //创建Connection对象
if(SUCCEEDED(hr))
{
CStdioFile mFile;
CString connstr="";
mFile.Open("a.txt",CFile::modeRead);
if(mFile.ReadString(connstr))
MessageBox("获得SQL字符串!","成功",MB_OK);
m_pConnection->Open((_bstr_t)connstr,"","",adModeUnknown);//在这里要进行类型转换
//AfxMessageBox((_bstr_t)connstr);
MessageBox("连接SQL运行成功!","成功",MB_OK);
}
}
a.txt中的数据库连接串:
Provider=SQLOLEDB.1;Server=808644DA83B446C/SQLEXPRESS;DATABASE=ChinabaseSQLServer;UID=sa;PWD=123;