大家看下下面的代码有问题吗,用的是ACCESS数据库,在ODBC里配置好了。
BOOL CDbAdoDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE; // TODO: add reinitialization code here
// (SDI documents will reuse this document)
m_strConnection=_T("Provider=MSDASQL.1;Data Sourse=person");
m_strCmdText=_T("select * from person");
m_pRs=NULL;
m_piAdoRecordBinding=NULL;
::CoInitialize(NULL);
try
{
m_pRs.CreateInstance(_uuidof(Recordset));
m_pRs->Open((LPCTSTR)m_strCmdText,(LPCTSTR)m_strConnection,
adOpenDynamic,adLockOptimistic,adCmdUnknown);
if (FAILED(m_pRs->QueryInterface(_uuidof(IADORecordBinding),
(LPVOID *)&m_piAdoRecordBinding)))
_com_issue_error(E_NOINTERFACE);
m_piAdoRecordBinding->BindToRecordset(&m_rsRecSet);
POSITION pos=GetFirstViewPosition(); CDbAdoView* pView=(CDbAdoView *)GetNextView(pos);
if (pView)
pView->RefreshBoundData();
}
catch (_com_error &e)
{
GenerateError(e.Error(),e.Description());
}
return TRUE;
}
运行时不能打开数据集,提示:数据源不对或者是数据库驱动名称找不到。
哪位能帮我看看吗?
BOOL CDbAdoDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE; // TODO: add reinitialization code here
// (SDI documents will reuse this document)
m_strConnection=_T("Provider=MSDASQL.1;Data Sourse=person");
m_strCmdText=_T("select * from person");
m_pRs=NULL;
m_piAdoRecordBinding=NULL;
::CoInitialize(NULL);
try
{
m_pRs.CreateInstance(_uuidof(Recordset));
m_pRs->Open((LPCTSTR)m_strCmdText,(LPCTSTR)m_strConnection,
adOpenDynamic,adLockOptimistic,adCmdUnknown);
if (FAILED(m_pRs->QueryInterface(_uuidof(IADORecordBinding),
(LPVOID *)&m_piAdoRecordBinding)))
_com_issue_error(E_NOINTERFACE);
m_piAdoRecordBinding->BindToRecordset(&m_rsRecSet);
POSITION pos=GetFirstViewPosition(); CDbAdoView* pView=(CDbAdoView *)GetNextView(pos);
if (pView)
pView->RefreshBoundData();
}
catch (_com_error &e)
{
GenerateError(e.Error(),e.Description());
}
return TRUE;
}
运行时不能打开数据集,提示:数据源不对或者是数据库驱动名称找不到。
哪位能帮我看看吗?
你参考一下建立连接的例子(SqlServer)
BOOL CreateConnect(void)
{ CString m_sPassword=_T("");
CString m_sUser=_T("anonymous");
CString m_sServer=_T("ug00");
//CString strConn="uid=anonymous;pwd=;driver={SQL Server};server=ug00;database=STDLIBRARY;dsn=''";
CString strConn;
//strConn.Format("uid=%s;pwd=%s;driver={SQL Server};server=%s;database='';dsn='SPL'",m_sPassword,m_sUser,m_sServer);
//strConn.Format("uid=%s;pwd=%s;driver={SQL Server};server=%s;database=STDLIBRARY;dsn=''",m_sPassword,m_sUser,m_sServer);
//建立连接串
strConn.Format("driver={SQL Server};server=%s;database=STDLIBRARY;dsn=''",m_sServer);
//CString strConn="Provider=SQLOLEDB;Persist Security Info=False;User ID="+g_Global.m_strUserName+";Initial Catalog=GLSTD_LIB;Data Source="+g_Global.m_strServerName+";Password="+g_Global.m_strPassword+";";//ZHANGSG+
HRESULT hr;
try{ /*----------------问题出在这里--------------*/
hr=gl_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
gl_pConnection->ConnectionTimeout=10;//连接时间
hr=gl_pConnection->Open(_bstr_t((LPCTSTR)strConn),
_bstr_t(L"anonymous"),
_bstr_t(L""),
adModeUnknown);
if(SUCCEEDED(hr))
{
//g_Global.bConnected=TRUE;
return TRUE;
}
}
}
catch(_com_error &e)//捕捉异常情况
{
#ifdef _DEBUG
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
CString csError;
csError.Format(":\n\t编号:%08x\n\t编号含义:%s\n\t源代码:%s\n\t描述:%s\n",
e.Error(),e.ErrorMessage(),(LPCTSTR)bstrSource,(LPCTSTR)bstrDescription);
MessageBox(NULL,(LPCTSTR)csError,"[Arx->BuidDbConnection]引进的对象出现异常",MB_OK|MB_ICONEXCLAMATION);
#endif
//g_Global.bConnected=FALSE;
return FALSE;
}
catch(...)
{
#ifdef _DEBUG
AfxMessageBox("不可捕捉的异常。",MB_OK|MB_ICONEXCLAMATION);
#endif
//g_Global.bConnected=FALSE;
return FALSE;
}
return FALSE;
}