急需一个VC++使用ADO连接SQL server数据库的案例,只需要连接到数据库就行,最好多写点注释。

2025-01-18 16:49:28
推荐回答(3个)
回答1:

//ADO连接,首先要加载ADO模块,然后再调用。

1,先创建数据库Temptest,新建一张表StuMessage,其中包含列ID、StuName、StuSex、StuAge、StuScore

2,建立C++类,类名例为ADO。
在ADO.h的#prama once后添加:#import "c:\program files\common files\system\ado\msado15.dll no_namespace rename("EOF", "adoEOF")rename("EOF", "adoEOF")
#include

class ADO
{
_RecordsetPtr m_pRecordset;

public:
_ConnectionPtr m_pConnection;

ADO();
virtual ~ADO();
void Connect(void);
void ExitConnect(void);
_RecordsetPtr& GetRecordset(_bstr_t SQL);

};

3,ADO.cpp 在类中写入对应的方法。

ADO::ADO()
{
m_pConnection = NULL;
}

ADO::~ADO()
{

}

void ADO::Connect(void)
{
try{
cout<<"正在连接数据库,请稍等!"< ::CoInitialize(NULL); //初始化COM环境
m_pConnection.CreateInstance(__uuidof(Connection)); //创建连接对象
m_pConnection->ConnectionString="Provider=SQLOLEDB.1; Persist Security Info=True; User ID=sa; Password=12345; Initial Catalog=TempTest; Data Source=192.168.8.6"; //请将数据库参数相应ID、Password、地址更改
//连接服务器和数据库
HRESULT hr=m_pConnection->Open("", "", "", 0);
if(hr!=S_OK)
cout<<"不能连接到数据库,请确认数据库是否开启!"< else
cout<<"连接成功!"< }
catch(_com_error e){
cout<<(char *)e.Description()< }
}

void ADO::ExitConnect(void)
{
if(m_pRecordset!=NULL){
m_pRecordset->Close();
m_pConnection->Close();
}
::CoUninitialize(); //释放环境
}

_RecordsetPtr& ADO::GetRecordset(_bstr_t SQL)
{
m_pRecordset=NULL;
ADO link;
try{
if(m_pConnection==NULL)
Connect();
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open((_bstr_t)SQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch(_com_error e){
cout<<(char *)e.Description()< m_pRecordset=NULL;
return m_pRecordset;
}
return m_pRecordset;
}

4,主文件.cpp

// 数据库ADO.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ADO.h"

#include
#include
using namespace std;

int main(int argc, char* argv[])
{
printf("Hello World!\n");

string sql;
ADO record;
_RecordsetPtr m_pRecordset;

//增加
{
sql="select * from StuMessage";
_bstr_t bstr_t(sql.c_str());
m_pRecordset=record.GetRecordset(bstr_t);
m_pRecordset->AddNew();
m_pRecordset->PutCollect("ID", _variant_t("1"));
m_pRecordset->PutCollect("StuName", _variant_t("edward"));
m_pRecordset->PutCollect("StuSex", _variant_t("m"));
m_pRecordset->PutCollect("StrAge", _variant_t("17"));
m_pRecordset->PutCollect("StrScore", _variant_t("98"));
m_pRecordset->Update();
}

{
//插入
sql = "insert into StuMessage(ID,StuName,StuSex,StrAge,StrScore) values ('3','liuhai','l',19,97)";
_bstr_t bstr_t(sql.c_str());
_variant_t RecordsAffected;
record.m_pConnection->Execute(bstr_t,&RecordsAffected,adCmdText);
cout<<"插入成功!"< }

{
//修改
sql = "update StuMessage set ID='2',StuName='wangqing',StuSex='l',StrAge=18,StrScore=99 ";
_bstr_t bstr_t(sql.c_str());
_variant_t RecordsAffected;
record.m_pConnection->Execute(bstr_t,&RecordsAffected,adCmdText);
cout<<"修改成功!"< }

{
//删除
sql = "delete from StuMessage";
_bstr_t bstr_t(sql.c_str());
_variant_t RecordsAffected;
record.m_pConnection->Execute(bstr_t,&RecordsAffected,adCmdText);
cout<<"删除成功!"< }

record.ExitConnect();
return 0;
}

以上程序能够运行,并且可以操作SQL数据表中记录。

回答2:

度娘下vc ado 类,可以找到

回答3:

有的话发