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条回答

    报告相同问题?

    悬赏问题

    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法