#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF", "EndOfFile")
#include <iostream>
#include <iomanip> //for setw()
using namespace std;
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF", "EndOfFile")
#include <iostream>
#include <iomanip> //for setw()
using namespace std;
class player
{
public:
char pname[255];
char tname[255];
float appearnum;
float rebounds;
float assists;
float pscore;
public:
player(){}
~player(){}
};
int main()
{
player pla;
::CoInitialize(NULL); // 初始化OLE/COM库环境 ,为访问ADO接口做准备
// 创建记录集对象
_RecordsetPtr m_pRecordset("ADODB.Recordset");
//m_pRecordset.CreateInstance(__uuidof(Recordset));
//创建connection对象
_ConnectionPtr m_pConnection("ADODB.Connection");
//设置连接字符串
_bstr_t strConnect=("driver={SQL Server};Server=(local)\\sqlexpress;DATABASE=NBA;UID=sa;PWD=123");
//_bstr_t strConnect= "Provider=SQLOLEDB;Database=CAP; uid=sa; pwd=123;";
_bstr_t bstrSQL("select pname,tname from player"); //查询语句,表stu_info
std::cout<<"creating from database..."<<endl;
try
{
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!n";
_variant_t vpname,vtname; //对应库中的cid,cname,city,discnt
//,vappearnum,vrebounds,vassists,vpscore
while(1)
{
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
cout << "vpname vtname \n";
//vappearnum vrebounds vassists vpscore
cout<<"n----------------------------------------------------------------n\n";
while (!m_pRecordset->EndOfFile)
{
vpname = m_pRecordset->GetCollect(_variant_t((long)0));
vtname = m_pRecordset->GetCollect("tname");
// vappearnum = m_pRecordset->GetCollect("appearnum");
// vrebounds = m_pRecordset->GetCollect("rebounds");
// vassists = m_pRecordset->GetCollect("assists");
// vrebounds = m_pRecordset->GetCollect("rebounds");
if (vpname.vt != VT_NULL && vtname.vt != VT_NULL )
{//&& vappearnum.vt != VT_NULL && vrebounds.vt != VT_NULL && vassists.vt != VT_NULL && vpscore.vt != VT_NULL
cout.setf(ios::left);
cout << setw(20) << (char*)(_bstr_t)vpname;
cout << setw(8) << (char*)(_bstr_t)vtname;
// cout << setw(8) << (char*)(_bstr_t)vappearnum;
// cout <<setw(8) << (char*)(_bstr_t)vrebounds;
// cout << setw(8) << (char*)(_bstr_t)vassists;
// cout << setw(8) << (char*)(_bstr_t)vpscore;
cout.unsetf(ios::left);
cout << endl;
}
m_pRecordset->MoveNext(); ///移到下一条记录
}
cout << "n----------------------------------------------------------------n\n";
m_pRecordset->Update();
//m_pConnection->Execute(query_cmd,NULL,1); //用Execute执行sql语句来删除
m_pRecordset->Close(); // 关闭记录集
}
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
//cerr << "nERROR:" << (char*)e.Description();//抛出异常
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
//删除
::CoInitialize(NULL); // 初始化OLE/COM库环境 ,为访问ADO接口做准备
char * query_cmd1 = "DELETE FROM player WHERE pname = '凯文-杜兰特'";
_bstr_t bstrSQL1("select* from player");
//std::cout<<"creating from database..."<<endl;
try
{
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!n";
_variant_t vpname,vtname,vappearnum,vrebounds,vassists,vpscore; //对应库中的cid,cname,city,discnt
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pConnection->Execute(query_cmd1,NULL,1); //用Execute执行sql语句来删除
m_pRecordset->Update();
m_pRecordset->Close(); // 关闭记录集
std::cout<<"deleting from database..."<<endl;
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
//cerr << "nERROR:" << (char*)e.Description();//抛出异常
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
//增加
::CoInitialize(NULL); // 初始化OLE/COM库环境 ,为访问ADO接口做准备
char * query_cmd2 = "INSERT INTO player VALUES('凯文-杜兰特','雷霆',5,11.7,0.6,13.7)";
//std::cout<<"creating from database..."<<endl;
try
{
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!n";
_variant_t vpname,vtname,vappearnum,vrebounds,vassists,vpscore; //对应库中的cid,cname,city,discnt
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pConnection->Execute(query_cmd2,NULL,1);
m_pRecordset->Update();
m_pRecordset->Close(); // 关闭记录集
std::cout<<"adding from database..."<<endl;
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
cerr << "nERROR:" << (char*)e.Description();//抛出异常
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
return 0;
}
以上为表player的VC编程;
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF", "EndOfFile")
#include <iostream>
#include <iomanip> //for setw()
using namespace std;
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF", "EndOfFile")
#include <iostream>
#include <iomanip> //for setw()
using namespace std;
class team
{
public:
char cname[255];
char tname[255];
float twin;
float tlose;
public:
team(){}
~team(){}
};
int main()
{
team pla;
::CoInitialize(NULL);
_RecordsetPtr m_pRecordset("ADODB.Recordset");
//m_pRecordset.CreateInstance(__uuidof(Recordset));
_ConnectionPtr m_pConnection("ADODB.Connection");
_bstr_t strConnect=("driver={SQL Server};Server=(local)\\sqlexpress;DATABASE=NBA;UID=sa;PWD=123");
//_bstr_t strConnect= "Provider=SQLOLEDB;Database=CAP; uid=sa; pwd=123;";
_bstr_t bstrSQL("select * from team"); //stu_info
std::cout<<"creating from database..."<<endl;
try
{
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!n";
_variant_t vcname,vtname,vtwin,vtlose; //cid,cname,city,discnt
while(1)
{
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
cout << "vcname vtname vtwin vtlose \n";
cout<<"n----------------------------------------------------------------n\n";
while (!m_pRecordset->EndOfFile)
{
vcname = m_pRecordset->GetCollect(_variant_t((long)0));
vtname = m_pRecordset->GetCollect("tname");
vtwin = m_pRecordset->GetCollect("twin");
vtlose = m_pRecordset->GetCollect("tlose");
if (vcname.vt != VT_NULL && vtname.vt != VT_NULL && vtwin.vt != VT_NULL && vtlose.vt != VT_NULL )
{
cout.setf(ios::left);
cout << setw(20) << (char*)(_bstr_t)vtname;
cout << setw(8) << (char*)(_bstr_t)vtwin;
cout << setw(8) << (char*)(_bstr_t)vtlose;
cout <<setw(8) << (char*)(_bstr_t)vcname;
cout.unsetf(ios::left);
cout << endl;
}
m_pRecordset->MoveNext();
}
cout << "n----------------------------------------------------------------n\n";
m_pRecordset->Update();
//m_pConnection->Execute(query_cmd,NULL,1);
m_pRecordset->Close();
}
}
catch(_com_error e)
{
//cerr << "nERROR:" << (char*)e.Description();
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
::CoInitialize(NULL);
char * query_cmd1 = "DELETE FROM team WHERE cname = '迈克-马龙'";
//std::cout<<"creating from database..."<<endl;
try
{
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!n";
_variant_t vcname,vtname,vtwin,vtlose; //cid,cname,city,discnt
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pConnection->Execute(query_cmd1,NULL,1);
m_pRecordset->Update();
m_pRecordset->Close();
std::cout<<"deleting from database..."<<endl;
}
// 虏露脳陆脪矛鲁拢
catch(_com_error e)
{
// 脧脭脢戮麓铆脦贸脨脜脧垄
//cerr << "nERROR:" << (char*)e.Description();//脜脳鲁枚脪矛鲁拢
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
//脭枚录脫
::CoInitialize(NULL); // 鲁玫脢录禄炉OLE/COM驴芒禄路戮鲁 拢卢脦陋路脙脦脢ADO陆脫驴脷脳枚脳录卤赂
// 麓麓陆篓录脟脗录录炉露脭脧贸
//_RecordsetPtr m_pRecordset("ADODB.Recordset");
//m_pRecordset.CreateInstance(__uuidof(Recordset));
//麓麓陆篓connection露脭脧贸
// _ConnectionPtr m_pConnection("ADODB.Connection");
//脡猫脰脙脕卢陆脫脳脰路没麓庐
//_bstr_t strConnect=("driver={SQL Server};Server=(local)\\sqlexpress;DATABASE=CAP;UID=sa;PWD=123");
//_bstr_t strConnect= "Provider=SQLOLEDB;Database=NBA; uid=login; pwd=login;";
//_bstr_t bstrSQL("select * from team"); //虏茅脩炉脫茂戮盲拢卢卤铆stu_info
char * query_cmd2 = "INSERT INTO team VALUES('哔哩哔哩1',100,0,'姚明')";
//std::cout<<"creating from database..."<<endl;
try
{
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!n";
_variant_t vcname,vtname,vtwin,vtlose; //露脭脫娄驴芒脰脨碌脛cid,cname,city,discnt
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pConnection->Execute(query_cmd2,NULL,1);
m_pRecordset->Update();
m_pRecordset->Close(); // 鹿脴卤脮录脟脗录录炉
std::cout<<"adding from database..."<<endl;
}
// 虏露脳陆脪矛鲁拢
catch(_com_error e)
{
// 脧脭脢戮麓铆脦贸脨脜脧垄
cerr << "nERROR:" << (char*)e.Description();//脜脳鲁枚脪矛鲁拢
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
return 0;
}
以上是表team的连接代码
由于分工时两人修改的表不同,而组员用的是mac系统,所以再导入到windows系统时会出现乱码 (忽视这些乱码吧 意义不大)
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF", "EndOfFile")
#include <iostream>
#include <iomanip> //for setw()
using namespace std;
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF", "EndOfFile")
#include <iostream>
#include <iomanip> //for setw()
using namespace std;
class coach
{
public:
char cname[255];
char tname[255];
float cnum;
public:
coach(){}
~coach(){}
};
int main()
{
coach pla;
::CoInitialize(NULL); // 鲁玫脢录禄炉OLE/COM驴芒禄路戮鲁 拢卢脦陋路脙脦脢ADO陆脫驴脷脳枚脳录卤赂
// 麓麓陆篓录脟脗录录炉露脭脧贸
_RecordsetPtr m_pRecordset("ADODB.Recordset");
//m_pRecordset.CreateInstance(__uuidof(Recordset));
//麓麓陆篓connection露脭脧贸
_ConnectionPtr m_pConnection("ADODB.Connection");
//脡猫脰脙脕卢陆脫脳脰路没麓庐
_bstr_t strConnect=("driver={SQL Server};Server=(local)\\sqlexpress;DATABASE=NBA;UID=sa;PWD=123");
//_bstr_t strConnect= "Provider=SQLOLEDB;Database=CAP; uid=sa; pwd=123;";
_bstr_t bstrSQL("select * from coach"); //虏茅脩炉脫茂戮盲拢卢卤铆stu_info
std::cout<<"creating from database..."<<endl;
try
{
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!n";
_variant_t vcname,vtname,vcnum; //露脭脫娄驴芒脰脨碌脛cid,cname,city,discnt
while(1)
{
// 脠隆碌脙卤铆脰脨碌脛录脟脗录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
cout << "vcname vtname vcnum \n";
cout<<"n----------------------------------------------------------------n\n";
while (!m_pRecordset->EndOfFile)
{
vcname = m_pRecordset->GetCollect(_variant_t((long)0));
vtname = m_pRecordset->GetCollect("tname");
vcnum = m_pRecordset->GetCollect("cnum");
if (vcname.vt != VT_NULL && vtname.vt != VT_NULL && vcnum.vt != VT_NULL)
{
cout.setf(ios::left);
cout << setw(20) << (char*)(_bstr_t)vcname;
cout << setw(8) << (char*)(_bstr_t)vtname;
cout << setw(8) << (char*)(_bstr_t)vcnum;
cout.unsetf(ios::left);
cout << endl;
}
m_pRecordset->MoveNext(); ///脪脝碌陆脧脗脪禄脤玫录脟脗录
}
cout << "n----------------------------------------------------------------n\n";
m_pRecordset->Update();
//m_pConnection->Execute(query_cmd,NULL,1); //脫脙Execute脰麓脨脨sql脫茂戮盲脌麓脡戮鲁媒
m_pRecordset->Close(); // 鹿脴卤脮录脟脗录录炉
}
}
// 虏露脳陆脪矛鲁拢
catch(_com_error e)
{
// 脧脭脢戮麓铆脦贸脨脜脧垄
//cerr << "nERROR:" << (char*)e.Description();//脜脳鲁枚脪矛鲁拢
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
//脡戮鲁媒
::CoInitialize(NULL); // 鲁玫脢录禄炉OLE/COM驴芒禄路戮鲁 拢卢脦陋路脙脦脢ADO陆脫驴脷脳枚脳录卤赂
char * query_cmd1 = "DELETE FROM coach WHERE cname = '迈克-马龙1'";
//std::cout<<"creating from database..."<<endl;
try
{
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!n";
_variant_t vcname,vtname,vcnum,vassists,vpscore; //露脭脫娄驴芒脰脨碌脛cid,cname,city,discnt
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pConnection->Execute(query_cmd1,NULL,1); //脫脙Execute脰麓脨脨sql脫茂戮盲脌麓脡戮鲁媒
m_pRecordset->Update();
m_pRecordset->Close(); // 鹿脴卤脮录脟脗录录炉
std::cout<<"deleting from database..."<<endl;
}
// 虏露脳陆脪矛鲁拢
catch(_com_error e)
{
// 脧脭脢戮麓铆脦贸脨脜脧垄
//cerr << "nERROR:" << (char*)e.Description();//脜脳鲁枚脪矛鲁拢
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
//脭枚录脫
::CoInitialize(NULL); // 鲁玫脢录禄炉OLE/COM驴芒禄路戮鲁 拢卢脦陋路脙脦脢ADO陆脫驴脷脳枚脳录卤赂
// 麓麓陆篓录脟脗录录炉露脭脧贸
//_RecordsetPtr m_pRecordset("ADODB.Recordset");
//m_pRecordset.CreateInstance(__uuidof(Recordset));
//麓麓陆篓connection露脭脧贸
// _ConnectionPtr m_pConnection("ADODB.Connection");
//脡猫脰脙脕卢陆脫脳脰路没麓庐
//_bstr_t strConnect=("driver={SQL Server};Server=(local)\\sqlexpress;DATABASE=CAP;UID=sa;PWD=123");
//_bstr_t strConnect= "Provider=SQLOLEDB;Database=NBA; uid=login; pwd=login;";
//_bstr_t bstrSQL("select * from coach"); //虏茅脩炉脫茂戮盲拢卢卤铆stu_info
char * query_cmd2 = "INSERT INTO coach VALUES('迈克-马龙2',12,'掘金')";
//std::cout<<"creating from database..."<<endl;
try
{
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!n";
_variant_t vcname,vtname,vcnum,vassists,vpscore; //露脭脫娄驴芒脰脨碌脛cid,cname,city,discnt
// 脠隆碌脙卤铆脰脨碌脛录脟脗录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pConnection->Execute(query_cmd2,NULL,1);
m_pRecordset->Update();
m_pRecordset->Close(); // 鹿脴卤脮录脟脗录录炉
std::cout<<"adding from database..."<<endl;
}
// 虏露脳陆脪矛鲁拢
catch(_com_error e)
{
// 脧脭脢戮麓铆脦贸脨脜脧垄
cerr << "nERROR:" << (char*)e.Description();//脜脳鲁枚脪矛鲁拢
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
return 0;
}
以上是表coach的代码(是的又是乱码TAT)
以上做到了用C++将数据库与VC++连接,在VC++中成功修改了数据库中的元素。
运行结果:
表coach操作前
表coach操作后
其它表操作类似
操作前
操作后