我创建了一个对话框应用程序,我想在对话框弹出时连接数据库。
在对话框地DoModal()里地代码如下: _ConnectionPtr myconn ;
HRESULT hr;
hr = myconn.CreateInstance(_uuidof(Connection));
if(FAILED(hr))
{
MessageBox("over ","tttt",MB_OK);
return 0;
}
_bstr_t strConn = "Provider = SQLOLEDB;server = eamserver;database=liso;"
"uid = sa;pwd ="; try
{
myconn->Open(strConn,"","",NULL);
}
catch(_com_error &e)
{
MessageBox(e.Description(),"djfd",MB_OK);
} return CDialog::DoModal();------------------------------
结果总是myconn创建失败,为什么呀,谁能帮我该一该????????
在对话框地DoModal()里地代码如下: _ConnectionPtr myconn ;
HRESULT hr;
hr = myconn.CreateInstance(_uuidof(Connection));
if(FAILED(hr))
{
MessageBox("over ","tttt",MB_OK);
return 0;
}
_bstr_t strConn = "Provider = SQLOLEDB;server = eamserver;database=liso;"
"uid = sa;pwd ="; try
{
myconn->Open(strConn,"","",NULL);
}
catch(_com_error &e)
{
MessageBox(e.Description(),"djfd",MB_OK);
} return CDialog::DoModal();------------------------------
结果总是myconn创建失败,为什么呀,谁能帮我该一该????????
CoInitialize() or AfxOleInit();
加入到StdAfx.h中。将 AfxOleInit();
加入到 CDlgApp的InitInstatance()中。将你上面的代码放到CDlg的OnInitDialog()中。