VS2005 连接 SQL2000

来源:互联网 发布:天天特价9.9淘宝网灰枣 编辑:程序博客网 时间:2024/05/18 10:09

新建数据库MyTest,建表t_Test,

SQL 安全性给角色 分数据库权限,然后运行下列代码,数据库连接就如此的简单

#import "c:/Program Files/Common Files/System/ADO/msado15.dll" /
   no_namespace rename("EOF", "EndOfFile")

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string>
using namespace std;


void main()
{
 CoInitialize(NULL);

 try
 {
  _ConnectionPtr pConn("ADODB.Connection");
   _bstr_t strConnect= "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=sa;Initial Catalog=MyTest;Data Source=127.0.0.1";
  pConn->Open(strConnect,"","",adModeUnknown);
  _RecordsetPtr pRs("ADODB.Recordset");
  pRs->Open("select * from t_Test", _variant_t(pConn,true), adOpenStatic, adLockOptimistic, adCmdText);


  while( !pRs->EndOfFile )
  {
   cout<<"name= "<<_bstr_t(pRs->GetCollect("Name"))<<"/t";
   cout<<"ID= "<<_bstr_t(pRs->GetCollect("ID"))<<"/t";
   cout<<"Value= "<<_bstr_t(pRs->GetCollect("Value"))<<"/n";
   pRs->MoveNext();

  }
  pRs->Close();
  pConn->Close();
 }

 catch(_com_error &e)
 {
  cout<<e.Error ()<<endl;

 }
 int a;
 scanf("sssssssssssss",&a);
 
}

 

原创粉丝点击