急需一个VC++使用ADO连接SQL server数据库的案例,只需要连接到数据库就行,最好多写点注释。
用VS2003做出来的最好,有的话发QQ786080178@qq.com回答的满意的话再加分...
用VS2003做出来的最好,有的话发QQ786080178@qq.com回答的满意的话 再加分
展开
3个回答
展开全部
//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 <icrsint.h>
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<<"正在连接数据库,请稍等!"<<endl;
::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<<"不能连接到数据库,请确认数据库是否开启!"<<endl;
else
cout<<"连接成功!"<<endl;
}
catch(_com_error e){
cout<<(char *)e.Description()<<endl;
}
}
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()<<endl;
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 <iostream>
#include <string>
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<<"插入成功!"<<endl;
}
{
//修改
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<<"修改成功!"<<endl;
}
{
//删除
sql = "delete from StuMessage";
_bstr_t bstr_t(sql.c_str());
_variant_t RecordsAffected;
record.m_pConnection->Execute(bstr_t,&RecordsAffected,adCmdText);
cout<<"删除成功!"<<endl;
}
record.ExitConnect();
return 0;
}
以上程序能够运行,并且可以操作SQL数据表中记录。
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 <icrsint.h>
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<<"正在连接数据库,请稍等!"<<endl;
::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<<"不能连接到数据库,请确认数据库是否开启!"<<endl;
else
cout<<"连接成功!"<<endl;
}
catch(_com_error e){
cout<<(char *)e.Description()<<endl;
}
}
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()<<endl;
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 <iostream>
#include <string>
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<<"插入成功!"<<endl;
}
{
//修改
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<<"修改成功!"<<endl;
}
{
//删除
sql = "delete from StuMessage";
_bstr_t bstr_t(sql.c_str());
_variant_t RecordsAffected;
record.m_pConnection->Execute(bstr_t,&RecordsAffected,adCmdText);
cout<<"删除成功!"<<endl;
}
record.ExitConnect();
return 0;
}
以上程序能够运行,并且可以操作SQL数据表中记录。
追问
可不可一把做出来的项目给我发一份,我新手,很多地方都不知道。发我邮箱就行了,如果可以的话分全是你的。而且您给的代码有问题 调试的时候报错 ,您最好把整体项目给我 省的那么麻烦。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询