bin104959 2014-05-11 04:39
浏览 824

求大神帮忙,ado连接数据库sql server 2005,总是失败

#include
#include
#include
#include
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","ADOEOF")
int main()
{
if(!AfxOleInit())
{
AfxMessageBox("can't open the database!");
return 0;
}
_ConnectionPtr con;
_RecordsetPtr res;
con.CreateInstance("ADODB.Connection");
res.CreateInstance("ADODB.Recordset");
try{
CString connectstring="Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=jwglxt;Data Source=N36OUYVKXWZDR2G\SQLEXPRESS";
con->Open((_bstr_t)connectstring,"","",adModeUnknown);
}
catch(_com_error)
{
MessageBox(0,"make connection lost","connection lost",MB_OK);
return;
}
CString strsql="select * from student";
res.CreateInstance(_uuidof(Recordset));
res->Open((_bstr_t)strsql,con.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
printf("sno\t\tsname\t\tsage\t\tbir\n");
while(!res->BOF)
{
CString no;
CString name;
CString sex;
CString bir;
no=(CString)res->GetCollect("Sno").bstrVal;
name=(CString)res->GetCollect("Sname").bstrVal;
sex=(CString)res->GetCollect("Ssex").bstrVal;
bir=(CString)res->GetCollect("Sbir").bstrVal;
printf("%s\t%s\t%s\t%s\n",no,name,sex,bir);
res->MoveNext();
}
if(res!=NULL)
{
res->Close();
con->Close();
}
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器