高手帮忙看看源程序,万分感谢,程序运行说无效指针,在pConnection->Open这句出错

zsubob 2004-05-04 11:32:42
#include <iostream>
#include "stdafx.h"
int main()
{
_ConnectionPtr pConnection;
_RecordsetPtr pRecordset;
_variant_t TheValue;
if(!AfxOleInit())
{
std::cout<<"COM Init error\n";
return -1;
}
pConnection.CreateInstance(__uuidof(Connection));
pRecordset.CreateInstance (__uuidof(Recordset));
try
{
pConnection->Open ("driver = {SQL Server};Server = 127.0.0.1;DATABASE = master;UID = sa;PWD = viton","","",adModeUnknown); pRecordset->Open ("select name from sysdatabase",_variant_t((IDispatch*)pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
while(!pRecordset->adoEOF)
{
TheValue = pRecordset->GetCollect ("name");
if(TheValue.vt != VT_NULL)
std::cout<<(char*)_bstr_t(TheValue)<<'\n';
pRecordset->MoveNext ();
}
}catch(_com_error &e)
{
std::cout<<e.ErrorMessage ()<<'\n';
return -1;
}
pRecordset->Close();
pConnection->Close ();
return 0;
}

...全文
28 3 点赞 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zsubob 2004-05-04
谢谢,谢谢,真得很感谢
  • 打赏
  • 举报
回复
醉马不肖 2004-05-04
CoInitialize(NULL);
CoUninitialize();
  • 打赏
  • 举报
回复
醉马不肖 2004-05-04
#include <iostream>
#include "stdafx.h"
int main()
{
CoInitialize(NULL);
{
_ConnectionPtr pConnection;
_RecordsetPtr pRecordset;
_variant_t TheValue;
if(!AfxOleInit())
{
std::cout<<"COM Init error\n";
return -1;
}
pConnection.CreateInstance(__uuidof(Connection));
pRecordset.CreateInstance (__uuidof(Recordset));
try
{
pConnection->Open ("driver = {SQL Server};Server = 127.0.0.1;DATABASE = master;UID = sa;PWD = viton","","",adModeUnknown); pRecordset->Open ("select name from sysdatabase",_variant_t((IDispatch*)pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
while(!pRecordset->adoEOF)
{
TheValue = pRecordset->GetCollect ("name");
if(TheValue.vt != VT_NULL)
std::cout<<(char*)_bstr_t(TheValue)<<'\n';
pRecordset->MoveNext ();
}
}catch(_com_error &e)
{
std::cout<<e.ErrorMessage ()<<'\n';
return -1;
}
pRecordset->Close();
pConnection->Close ();
}
CoUninitialize();
return 0;
}
  • 打赏
  • 举报
回复
相关推荐
发帖
数据库
加入

3957

社区成员

VC/MFC 数据库
申请成为版主
帖子事件
创建了帖子
2004-05-04 11:32
社区公告
暂无公告