2 u010358288 u010358288 于 2014.05.11 12:39 提问

求大神帮忙,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();
}
}

Csdn user default icon
上传中...
上传图片
插入图片