关于ADO访问SQL数据库的问题。

zjkwell 2003-03-11 04:30:58
请看下面这段代码:

void CTestDlg::OnConnect()
{
strcpy(g_dbinfo.str_dbip,"192.168.1.253");
strcpy(g_dbinfo.str_DBNAME,"zjk");
strcpy(g_dbinfo.str_DBUSER,"sa");
strcpy(g_dbinfo.str_DBPASS,"sa");

DisConnectDB(g_pConRecv);



variant_t vRowaffected;
CString strsql;
strsql.Format("select * from worker");
_bstr_t b_sql=_bstr_t(strsql);
try
{g_pConRecv->BeginTrans();
g_pConRecv->Execute(b_sql,&vRowaffected,adCmdText);
g_pConRecv->CommitTrans();
MessageBox("Update Successfully!");
}
catch(_com_error err)
{CString str;
str.Format("错误信息是%s",err.ErrorMessage());
str.Format("错误描述是%s",(char *)err.Description());
DisConnectDB(g_pConRecv);
ConnectDB(g_dbinfo,g_pConRecv);
::AfxMessageBox("ExeCute TS Error 01!"); }


}



BOOL ConnectDB(DBINFO dbinfo,_ConnectionPtr& pCon)
{


HRESULT hr;
hr= pCon.CreateInstance(__uuidof(Connection));

if(FAILED(hr))
{
return FALSE;
}
CString temp="";
temp.Format("Provider=SQLOLEDB;Persist Security Info=False;Initial Catalog=%s;Server=%s;Auto Translate=False",dbinfo.str_DBNAME,dbinfo.str_dbip);
try
{
pCon->Open(_bstr_t(temp),_bstr_t(dbinfo.str_DBUSER),_bstr_t(dbinfo.str_DBPASS), -1);
}
catch(_com_error err)
{
::AfxMessageBox("Connect error.");
CString str;
str="连接数据库失败";
str.Format("错误信息是%s",err.ErrorMessage());
str.Format("错误描述是%s",(char *)err.Description());
return FALSE;
}

catch(...)
{
return FALSE;
}
return TRUE;
}

void DisConnectDB(_ConnectionPtr& pCon)
{
if(pCon!=NULL)
{pCon.Release();
pCon=NULL;
}
return;
}
编译没有错误,但是执行时却报告g_pConRecv指针无效,我觉得该指针没有被正确的负值,但是在另一个程序中这样用就可以正常运行,请问怎样给g_pConRecv指针负值?
...全文
39 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

4,017

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧