网上很多相关完整资料,搜“VC ADO MYSQL”,灰常多。
下面是一个比较精炼的描述:
1、应用程序的stdafx.h头文件中(也可以在其他合适的地方)包含如下语句。
#import "C://program files//common files//system//ado//msado15.dll"
no_namespace rename ("EOF", "adoEOF")
2、链接数据库
CoInitialize(NULL)
//初始化Com组件
_ConnectionPtr
conPtr//数据库链接指针
//
conPtr.CreateInstance("ADODB.Connection")
//Connection用于与数据库服务器的链接
conPtr.CreateInstance(__uuidof(Connection))
//Connection用于与数据库服务器的链接另一种方式
try
{
conPtr->ConnectionTimeout = 5//设置连接时间
//
MySqlTest为数据源名 localhost表示本地
root表示用户名 sa表示密码
//链接方式1,使用这种方式链接时要注意在设置数据源时一定要选择一个数据库
//conPtr->Open("DSN=MySqlTestserver=localhost","root","sa",adModeUnknown)///连接MySql数据库(测试成功)
//链接方式2采用这种方式链接时在创建数据源时没有必要选择一个数据库 conPtr->Open("DSN=MySqlTestserver=localhostdatabase=test","root","sa",adModeUnknown)///连接MySql数据库(测试成功)
}
catch(_com_error e) //捕捉异常
{
AfxMessageBox(e.ErrorMessage())
}
CoUninitialize()//释放com组件//不知是否有必要
3、访问数据库
_RecordsetPtr recordPtr//数据集指针
recordPtr.CreateInstance(__uuidof(Recordset))
CString
cmdStr=_T("select name,stuno from
student")//student为表名 该表中有两个字段name(字符型)stuno(int型)
try
{
recordPtr->Open(_variant_t(cmdStr),
conPtr.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText)
}
catch
(_com_error e)
{
AfxMessageBox(e.Description())
}
while
(!(recordPtr->adoEOF))
{//获取记录
CString
name=(CString)recordPtr->GetCollect(_variant_t("name")).bstrVal
int
no=recordPtr->GetCollect(_variant_t("stuno")).intVal
recordPtr->MoveNext()
}
recordPtr->Close()//关闭记录集
recordPtr.Release()//释放空间
conPtr->Close()//关闭连接
conPtr.Release()//释放空间
//下面的代码是一个实现C++连接MYSQL数据库的很好的例子//这里用了建表,插入,检索,删表等常用功能//我用VC++6.0生成,已经成功连接了。//在VC++6.0中要想把做一下两步准备工作才可以。//(1)Tools->Options->Directories->Include files中添加C:\Program Files\MySQL\MySQL Server 6.0\include//(2)Tools->Options->Directories->Library files中添加C:\Program Files\MySQL\MySQL Server 6.0\lib\opt//其实就是将头文件和库文件包含进来//我觉得这个例子写的很好,再结合自己的试验,特地介绍给大家! #include <winsock.h>#include <iostream>#include <string>#include <mysql.h>using namespace std#pragma comment(lib, "ws2_32.lib")#pragma comment(lib, "libmysql.lib") //单步执行,不想单步执行就注释掉#define STEPBYSTEP int main() {cout <<"****************************************" <<endl#ifdef STEPBYSTEPsystem("pause")#endif //必备的一个数据结构MYSQL mydata//初始化数据库if (0 == mysql_library_init(0, NULL, NULL)) {cout <<"mysql_library_init() succeed" <<endl } else {cout <<"mysql_library_init() failed" <<endl return -1 } #ifdef STEPBYSTEPsystem("pause")#endif //初始化数据结构if (NULL != mysql_init(&mydata)) {cout <<"mysql_init() succeed" <<endl } else {cout <<"mysql_init() failed" <<endl return -1 } #ifdef STEPBYSTEPsystem("pause")#endif //在连接数据库之前,设置额外的连接选项//可以设置的选项很多,这里设置字符集,否则无法处理中文if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) {cout <<"mysql_options() succeed" <<endl } else {cout <<"mysql_options() failed" <<endl return -1 } #ifdef STEPBYSTEPsystem("pause")#endif //连接数据库if (NULL!= mysql_real_connect(&mydata, "localhost", "root", "test", "test",3306, NULL, 0))//这里的地址,用户名,密码,端口可以根据自己本地的情况更改{cout <<"mysql_real_connect() succeed" <<endl } else {cout <<"mysql_real_connect() failed" <<endl return -1 } #ifdef STEPBYSTEPsystem("pause")#endif //sql字符串string sqlstr//创建一个表sqlstr = "CREATE TABLE IF NOT EXISTS user_info" sqlstr += "(" sqlstr +="user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID'," sqlstr +="user_name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User'," sqlstr +="user_second_sum INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'" sqlstr += ")" if (0 == mysql_query(&mydata, sqlstr.c_str())) {cout <<"mysql_query() create table succeed" <<endl } else {cout <<"mysql_query() create table failed" <<endl mysql_close(&mydata) return -1 } #ifdef STEPBYSTEPsystem("pause")#endif //向表中插入数据sqlstr ="INSERT INTO user_info(user_name) VALUES('公司名称'),('一级部门'),('二级部门'),('开发小组'),('姓名')" if (0 == mysql_query(&mydata, sqlstr.c_str())) {cout <<"mysql_query() insert data succeed" <<endl } else {cout <<"mysql_query() insert data failed" <<endl mysql_close(&mydata) return -1 } #ifdef STEPBYSTEPsystem("pause")#endif //显示刚才插入的数据sqlstr = "SELECT user_id,user_name,user_second_sum FROM user_info" MYSQL_RES *result = NULL if (0 == mysql_query(&mydata, sqlstr.c_str())) {cout <<"mysql_query() select data succeed" <<endl//一次性取得数据集result = mysql_store_result(&mydata) //取得并打印行数int rowcount = mysql_num_rows(result) cout <<"row count: " <<rowcount <<endl//取得并打印各字段的名称unsigned int fieldcount = mysql_num_fields(result) MYSQL_FIELD *field = NULL for (unsigned int i = 0i <fieldcounti++) {field = mysql_fetch_field_direct(result, i) cout <<field->name <<"\t\t" }cout <<endl//打印各行MYSQL_ROW row = NULL row = mysql_fetch_row(result) while (NULL != row) {for (int i = 0i <fieldcounti++) {cout <<row[i] <<"\t\t" }cout <<endl row = mysql_fetch_row(result) } } else {cout <<"mysql_query() select data failed" <<endl mysql_close(&mydata) return -1 } #ifdef STEPBYSTEPsystem("pause")#endif //删除刚才建的表sqlstr = "DROP TABLE user_info" if (0 == mysql_query(&mydata, sqlstr.c_str())) {cout <<"mysql_query() drop table succeed" <<endl } else {cout <<"mysql_query() drop table failed" <<endl mysql_close(&mydata) return -1 }mysql_free_result(result) mysql_close(&mydata) mysql_server_end()system("pause") return 0}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)