一个简单的数据库连接为什么不成功w

kezhon 2008-03-26 06:25:23
void CAdoDlg::OnBtnQuery() 
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));

pConn->ConnectionString="Data Source=KEZHON-PC\SQLEXPRESS;Initial Catalog=Love;Integrated Security=True";
//连接字符串是在vs2008中建立了一个连接,然后把连接字符串复制过来得到的
pConn->Open("","","",adConnectUnspecified);

pRst=pConn->Execute("select * from nets",NULL,adCmdText);

while(!pRst->EOF)
{
((CListBox *)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("id"));
pRst->MoveNext();
}
pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();
}



编译成功,运行就出问题,大侠帮忙看看,谢谢!
...全文
94 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
meiZiNick 2008-05-01
  • 打赏
  • 举报
回复
这个简单啊,网上搜一下就得到答案了.
UltraBejing 2008-04-30
  • 打赏
  • 举报
回复
接分先!
kezhon 2008-03-27
  • 打赏
  • 举报
回复

try
{
pConn->Open("","","",adConnectUnspecified);//问题应该出在这一步......

}
catch(_com_error e)
{
MessageBox(e.Description());
}


我把try...catch的范围缩小到pConn->Open()这一句代码时会弹出“多步OLE DB操作产生错误。如果可能,请检查每个OLE DB状态值。没有工作被完成。” 的错误提示。try...catch其他的语句则不会捕捉到异常提示。
kezhon 2008-03-27
  • 打赏
  • 举报
回复
void CAdoDlg::OnBtnQuery()
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));


pConn->ConnectionString="Data Source=KEZHON-PC\\SQLEXPRESS;Initial Catalog=Love;Integrated Security=True";

try
{
pConn->Open("","","",adConnectUnspecified);//问题应该出在这一步......

}
catch(_com_error e)
{
MessageBox(e.Description());
}
pRst=pConn->Execute("select * from nets",NULL,adCmdText);


while(!pRst->rsEOF)
{
((CListBox *)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("id"));
pRst->MoveNext();
}
pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();
}
kezhon 2008-03-27
  • 打赏
  • 举报
回复
真抱歉,犯了个这么愚蠢的错误。
try...catch捕获的错误是“多步OLE DB操作产生错误。如果可能,请检查每个OLE DB状态值。没有工作被完成。”
这是什么意思。
shakaqrj 2008-03-27
  • 打赏
  • 举报
回复
MessageBox(e.Description())
kezhon 2008-03-27
  • 打赏
  • 举报
回复

void CAdoDlg::OnBtnQuery()
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));

try
{
pConn->ConnectionString="Data Source=KEZHON-PC\\SQLEXPRESS;Initial Catalog=Love;Integrated Security=True";

//"Data Source=KEZHON-PC\\SQLEXPRESS;Initial Catalog=Love;Integrated Security=True"
pConn->Open("","","",adConnectUnspecified);

pRst=pConn->Execute("select * from nets",NULL,adCmdText);
}
catch(_com_error e)
{
MessageBox(LPCTSTR(_bstr_t(e.Description));//编译出错的行
}

while(!pRst->rsEOF)
{
((CListBox *)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("id"));
pRst->MoveNext();
}
pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();
}


编译错误:
C:\Users\kezhon\Desktop\PropertyInDlg\Ado\AdoDlg.cpp(193) : error C2440: 'type cast' : cannot convert from '' to 'class _bstr_t'
No constructor could take the source type, or constructor overload resolution was ambiguous
C:\Users\kezhon\Desktop\PropertyInDlg\Ado\AdoDlg.cpp(193) : error C2143: syntax error : missing ')' before ';'
shakaqrj 2008-03-27
  • 打赏
  • 举报
回复
try{
...
}catch(_com_error e){
messagebox(e.description)
}
kezhon 2008-03-27
  • 打赏
  • 举报
回复
双斜杠后编译无错,运行的时候,一点击“查询”,就弹出对话框“Debug Error!”,问中止,重试还是忽略,点击忽略,则弹出对话框曰“Runtime Error!”
shakaqrj 2008-03-27
  • 打赏
  • 举报
回复
双斜扛后抱什么错??
kezhon 2008-03-27
  • 打赏
  • 举报
回复
您真是高人啊,我照你的方法已經成功了,真是太太感謝了!!
shakaqrj 2008-03-27
  • 打赏
  • 举报
回复
你的连接字符串...
不知道你连的是什么数据库
新建一个udl文件,配置,最后用记事本打开,里面的就是连接字符串
kezhon 2008-03-26
  • 打赏
  • 举报
回复
不太明白您的意思,新手,能不能说详细点。
MBWQ 2008-03-26
  • 打赏
  • 举报
回复
pConn->Open("","","",adConnectUnspecified);
低级错误
kezhon 2008-03-26
  • 打赏
  • 举报
回复
两个也不行啊
zaodt 2008-03-26
  • 打赏
  • 举报
回复

PC\\SQLEXPRESS


两个\\
oakfire 2008-03-26
  • 打赏
  • 举报
回复
try catch看下出错信息
kezhon 2008-03-26
  • 打赏
  • 举报
回复
在连接字符串那一行有一个警告:
AdoDlg.cpp(180) : warning C4129: 'S' : unrecognized character escape sequence

4,017

社区成员

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

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