问题描述
- 求大神帮忙,ado连接数据库sql server 2005,总是失败
-
#include
#include
#include
#include
#import "C:Program FilesCommon FilesSystemadomsado15.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=N36OUYVKXWZDR2GSQLEXPRESS";
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("snottsnamettsagettbirn");
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("%st%st%st%sn",no,name,sex,bir);
res->MoveNext();
}
if(res!=NULL)
{
res->Close();
con->Close();
}
}
时间: 2024-12-24 21:04:59