2个回答
展开全部
下文程序中介绍了在C/C++中使用API以及ADO指针来连接这3种数据库的方式.因为ADO是最通用的数据库连接方式,兼容性比较好,特别推荐.
特别说明:
1.用微软的东西连接MySQL需要到www.mysql.com下载myODBC方可使用,毕竟是SUN的东西,需要myODBC转接.经测试,函数GetRecordCount()在操作MySQL时不可用,替代方式可以用一个遍历计数的算法,但是直接用封装好的函数实现暂且不行.
2.MS Office Access功能很强大,对于中小型项目开发绰绰有余,大型程序才需要用到MS SQL Server和Oracal.并且在windows环境开发下非常方便,有许多封装好的数据库操作函数.
3.请注意指定自己机器上msado15.dll动态联结库的位置,没有的话自己去搞一个去.毕竟ADO是定义在它上面的.
4.不明白函数请咨询MSDN.
#include <stdio.h>
#include <iostream>
using namespace std;
#import "C:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
int Initialize();
void Uninitialize();
// 数据库连接指针
_ConnectionPtr sg_pDBConn;
// 记录集指针
_RecordsetPtr sg_pDBRset;
int main(void)
{
char str[1024]={0};
_variant_t field;
_variant_t nfield;
long cnt = 0;
Initialize();
try
{
sg_pDBConn.CreateInstance(__uuidof(Connection));
sg_pDBRset.CreateInstance(__uuidof(Recordset));
}
catch (char *e)
{
cout<<"------------------------------------错误---------------------------"<<endl;
cout<<e<<endl;
return -1;
}
// Connect Data Base
//-------------For SQL SERVER
//DBServer = "Driver=SQL Server; Server=" + SQL_SERVER_IPADDR + ";
// Database=" + DATABASE_NAME + "; UID=" + SQL_SERVER_USERNAME + "; PWD=" + SQL_SERVER_PASSWORD
//-------------For MySQL SERVER--------------------------------------
// localhost or 192.168.1.166
strcat(str, "Data Source=mysql odbc 3.51driver; Server=localhost; ");
strcat(str, "Database=1; ID=root; Password=111111");
//-------------For ACCESS SERVER
//strcpy(str, "Provider = Microsoft.Jet.OLEDB.4.0; Data Source= ");
//strcat(str, "F:\\Program\\MySQL\\TestMySQL\\1.mdb");
try
{
sg_pDBConn->Open(str, "", "", adModeUnknown);
}
catch (char *e)
{
cout<<"------------------------------------连接失败---------------------------"<<endl;
cout<<e<<endl;
return -1;
}
// Get Data
strcpy(str, "select * from hello1");
try
{
sg_pDBRset->Open(str,
_variant_t((IDispatch *)sg_pDBConn,true),
adOpenStatic,
adLockOptimistic,
adCmdText);
}
catch (char *e)
{
cout<<"------------------------------------读数据失败---------------------------"<<endl;
cout<<e<<endl;
return -1;
}
sg_pDBRset->MoveFirst();
while(!sg_pDBRset->adoEOF)
{
nfield = "sno";
field = sg_pDBRset->GetCollect(nfield);
cout<<(char *)_bstr_t(field)<<" ";
nfield = "age";
field = sg_pDBRset->GetCollect(nfield);
cout<<atoi((char *)_bstr_t(field))<<endl;
sg_pDBRset->MoveNext();
}
Uninitialize();
return 0;
}
int Initialize()
{
if(S_OK != CoInitialize(NULL))
{
return -1;
}else{
return 0;
}
}
void Uninitialize()
{
CoUninitialize();
}
特别说明:
1.用微软的东西连接MySQL需要到www.mysql.com下载myODBC方可使用,毕竟是SUN的东西,需要myODBC转接.经测试,函数GetRecordCount()在操作MySQL时不可用,替代方式可以用一个遍历计数的算法,但是直接用封装好的函数实现暂且不行.
2.MS Office Access功能很强大,对于中小型项目开发绰绰有余,大型程序才需要用到MS SQL Server和Oracal.并且在windows环境开发下非常方便,有许多封装好的数据库操作函数.
3.请注意指定自己机器上msado15.dll动态联结库的位置,没有的话自己去搞一个去.毕竟ADO是定义在它上面的.
4.不明白函数请咨询MSDN.
#include <stdio.h>
#include <iostream>
using namespace std;
#import "C:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
int Initialize();
void Uninitialize();
// 数据库连接指针
_ConnectionPtr sg_pDBConn;
// 记录集指针
_RecordsetPtr sg_pDBRset;
int main(void)
{
char str[1024]={0};
_variant_t field;
_variant_t nfield;
long cnt = 0;
Initialize();
try
{
sg_pDBConn.CreateInstance(__uuidof(Connection));
sg_pDBRset.CreateInstance(__uuidof(Recordset));
}
catch (char *e)
{
cout<<"------------------------------------错误---------------------------"<<endl;
cout<<e<<endl;
return -1;
}
// Connect Data Base
//-------------For SQL SERVER
//DBServer = "Driver=SQL Server; Server=" + SQL_SERVER_IPADDR + ";
// Database=" + DATABASE_NAME + "; UID=" + SQL_SERVER_USERNAME + "; PWD=" + SQL_SERVER_PASSWORD
//-------------For MySQL SERVER--------------------------------------
// localhost or 192.168.1.166
strcat(str, "Data Source=mysql odbc 3.51driver; Server=localhost; ");
strcat(str, "Database=1; ID=root; Password=111111");
//-------------For ACCESS SERVER
//strcpy(str, "Provider = Microsoft.Jet.OLEDB.4.0; Data Source= ");
//strcat(str, "F:\\Program\\MySQL\\TestMySQL\\1.mdb");
try
{
sg_pDBConn->Open(str, "", "", adModeUnknown);
}
catch (char *e)
{
cout<<"------------------------------------连接失败---------------------------"<<endl;
cout<<e<<endl;
return -1;
}
// Get Data
strcpy(str, "select * from hello1");
try
{
sg_pDBRset->Open(str,
_variant_t((IDispatch *)sg_pDBConn,true),
adOpenStatic,
adLockOptimistic,
adCmdText);
}
catch (char *e)
{
cout<<"------------------------------------读数据失败---------------------------"<<endl;
cout<<e<<endl;
return -1;
}
sg_pDBRset->MoveFirst();
while(!sg_pDBRset->adoEOF)
{
nfield = "sno";
field = sg_pDBRset->GetCollect(nfield);
cout<<(char *)_bstr_t(field)<<" ";
nfield = "age";
field = sg_pDBRset->GetCollect(nfield);
cout<<atoi((char *)_bstr_t(field))<<endl;
sg_pDBRset->MoveNext();
}
Uninitialize();
return 0;
}
int Initialize()
{
if(S_OK != CoInitialize(NULL))
{
return -1;
}else{
return 0;
}
}
void Uninitialize()
{
CoUninitialize();
}
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
展开全部
1、数据库链接
int cppDatebase::DatabaseConnect(sBit8 *uName,sBit8 *pWord,sBit8 *dbName)
{
dbHandle = mysql_init(NULL);
if(NULL == dbHandle )
{
cout<<"mysql init error!"<<endl;
return (-1);
}
if(NULL == mysql_real_connect(dbHandle ,"localhost",uName,pWord,dbName,0,NULL,0))
{
cout<<"connect error!"<<endl;
return (-1);
}
mysql_set_character_set(dbHandle ,"utf8");
return 0;
}
2、数据库插入——增
int cppDatebase::DatabaseInsert(const char *cmd)
{
if(NULL == cmd)
{
cout<<"[insert] cmd error"<<endl;
return (-1);
}
int ret = mysql_query(dbHandle,cmd);
if(ret !=0)
{
cout << "Database Insert Info: exist, I am updata." <<endl<<endl;
return (-1);
}
return 0;
}
3、数据库删除——删
int cppDatebase::DatabaseDelete(const char *cmd)
{
if(NULL == cmd)
{
cout<<"[delete] cmd error"<<endl;
return (-1);
}
int ret = mysql_query(dbHandle,cmd);
if(ret !=0)
{
cout << "Database Delete Info: not exist." <<endl<<endl;
return (-1);
}
return 0;
}
4、数据库更新——改
int cppDatebase::DatabaseUpdate(const char *cmd)
{
if(NULL == cmd)
{
cout<<"[update] cmd error"<<endl;
return (-1);
}
int ret = mysql_query(dbHandle,cmd);
if(ret !=0)
{
cout << "Database Update Info: not exist, I am insert." <<endl<<endl;
return (-1);
}
return 0;
}
5、数据库查询——查
int cppDatebase::DatabaseQuery(char *cmd, char **row)
{
if(NULL == cmd || NULL == row)
{
cout<<"[query] cmd error"<<endl;
return (-1);
}
mysql_real_query(dbHandle,cmd,strlen(cmd));
MYSQL_RES *result = mysql_store_result(dbHandle);
if(result != NULL)
{
int numLine = mysql_num_rows(result);
int numList = mysql_num_fields(result);
}
else
{
cout<<"[query] mysql_store_result error !"<<endl;
return (-1);
}
while((row = mysql_fetch_row(result)))
{
printf("%s, %s %s %s\n",row[0],row[1],row[2],row[3]);
}
mysql_free_result(result);
return 0;
}
6、数据库断开
int cppDatebase::DatabaseClose()
{
mysql_close(dbHandle);
return 0;
}
int cppDatebase::DatabaseConnect(sBit8 *uName,sBit8 *pWord,sBit8 *dbName)
{
dbHandle = mysql_init(NULL);
if(NULL == dbHandle )
{
cout<<"mysql init error!"<<endl;
return (-1);
}
if(NULL == mysql_real_connect(dbHandle ,"localhost",uName,pWord,dbName,0,NULL,0))
{
cout<<"connect error!"<<endl;
return (-1);
}
mysql_set_character_set(dbHandle ,"utf8");
return 0;
}
2、数据库插入——增
int cppDatebase::DatabaseInsert(const char *cmd)
{
if(NULL == cmd)
{
cout<<"[insert] cmd error"<<endl;
return (-1);
}
int ret = mysql_query(dbHandle,cmd);
if(ret !=0)
{
cout << "Database Insert Info: exist, I am updata." <<endl<<endl;
return (-1);
}
return 0;
}
3、数据库删除——删
int cppDatebase::DatabaseDelete(const char *cmd)
{
if(NULL == cmd)
{
cout<<"[delete] cmd error"<<endl;
return (-1);
}
int ret = mysql_query(dbHandle,cmd);
if(ret !=0)
{
cout << "Database Delete Info: not exist." <<endl<<endl;
return (-1);
}
return 0;
}
4、数据库更新——改
int cppDatebase::DatabaseUpdate(const char *cmd)
{
if(NULL == cmd)
{
cout<<"[update] cmd error"<<endl;
return (-1);
}
int ret = mysql_query(dbHandle,cmd);
if(ret !=0)
{
cout << "Database Update Info: not exist, I am insert." <<endl<<endl;
return (-1);
}
return 0;
}
5、数据库查询——查
int cppDatebase::DatabaseQuery(char *cmd, char **row)
{
if(NULL == cmd || NULL == row)
{
cout<<"[query] cmd error"<<endl;
return (-1);
}
mysql_real_query(dbHandle,cmd,strlen(cmd));
MYSQL_RES *result = mysql_store_result(dbHandle);
if(result != NULL)
{
int numLine = mysql_num_rows(result);
int numList = mysql_num_fields(result);
}
else
{
cout<<"[query] mysql_store_result error !"<<endl;
return (-1);
}
while((row = mysql_fetch_row(result)))
{
printf("%s, %s %s %s\n",row[0],row[1],row[2],row[3]);
}
mysql_free_result(result);
return 0;
}
6、数据库断开
int cppDatebase::DatabaseClose()
{
mysql_close(dbHandle);
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询