Posted on 2009-07-24 20:21
Current 阅读(15160)
评论(5) 编辑 收藏 引用 所属分类:
数据库
通过自己摸索和网上帮助 了解了VC++ 用ADO 连接mysql数据库的方法:
使用的方法是利用ADO通过建立ODBC数据源来最终达到访问MySQL的目的。
1.安装mysql数据库服务器程序,也可安装appserv-win32-2.5.9.rar 里面包含了appche ,php,mysql ,安装后者可以通过网页管理数据库。因为方便所以我才用后者。

安装过程中记住密码。
2.建立数据库:
可以通过命令行 也可以通过网页建立:http://localhost/phpMyAdmin/ 输入用户名root 和密码就可通过网页管理数据库。
可以导入数据库:
-- phpMyAdmin SQL Dump
-- version 2.10.2
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2009 年 07 月 24 日 11:56
-- 服务器版本: 5.0.22
-- PHP 版本: 5.2.3
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- 数据库: `testmysql`
--
-- --------------------------------------------------------
--
-- 表的结构 `test`
--
CREATE TABLE `test` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=3 ;
--
-- 导出表中的数据 `test`
--
INSERT INTO `test` VALUES (1, 'qq');
INSERT INTO `test` VALUES (2, 'ww');
3.安装MyODBC 我安装的是:MyODBC-3.51.11-2-win.exe
然后:开始菜单->设置->控制面板->管理工具->数据源(ODBC)->系统DSN->添加->选择MySQL ODBC 3.51 Driver

这里可以填写所有的数据 以可以在程序里面填写数据 这里 DSN 为 ADOmysql
4.所有准备工作已做完,那我们就可以开始写程序连接数据库:
程序代码: 代码来源:《Visual C++ + SQL Server 数据库应用实例 完全解析》
1
// ADOConn.h: interface for the ADOConn class.
2
//
3
/**/////////////////////////////////////////////////////////////////////// 4
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
5
6
#if !defined(AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
7
#define AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_
8
9
#if _MSC_VER > 1000
10
#pragma once
11
#endif // _MSC_VER > 1000
12
13
class ADOConn
14

{
15
// 定义变量
16
public:
17
//添加一个指向Connection对象的指针:
18
_ConnectionPtr m_pConnection;
19
//添加一个指向Recordset对象的指针:
20
_RecordsetPtr m_pRecordset;
21
// 定义方法
22
public:
23
ADOConn();
24
virtual ~ADOConn();
25
// 初始化—连接数据库
26
void OnInitADOConn();
27
// 执行查询
28
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
29
// 执行SQL语句,Insert Update _variant_t
30
BOOL ExecuteSQL(_bstr_t bstrSQL);
31
void ExitConnect();
32
};
33
34
#endif // !defined(AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
35
1
// ADOConn.cpp: implementation of the ADOConn class.
2
//
3
/**/////////////////////////////////////////////////////////////////////// 4
5
#include "stdafx.h"
6
#include "ADOConn.h"
7
8
#ifdef _DEBUG
9
#undef THIS_FILE
10
static char THIS_FILE[]=__FILE__;
11
#define new DEBUG_NEW
12
#endif
13
14
/**/////////////////////////////////////////////////////////////////////// 15
// Construction/Destruction
16
/**/////////////////////////////////////////////////////////////////////// 17
18
ADOConn::ADOConn()
19

{
20
21
}
22
23
ADOConn::~ADOConn()
24

{
25
26
}
27
28
// 初始化—连接数据库
29
void ADOConn::OnInitADOConn()
30

{
31
// 初始化OLE/COM库环境
32
::CoInitialize(NULL);
33
34
try
35
{
36
// 创建Connection对象
37
m_pConnection.CreateInstance("ADODB.Connection");
38
m_pConnection->Open("DSN=ADOmysql;Server= localhost;Database=testmysql","root","123456",adModeUnknown);
39
}
40
// 捕捉异常
41
catch(_com_error e)
42
{
43
// 显示错误信息
44
AfxMessageBox(e.Description());
45
}
46
}
47
48
// 执行查询
49
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
50

{
51
try
52
{
53
// 连接数据库,如果Connection对象为空,则重新连接数据库
54
if(m_pConnection==NULL)
55
OnInitADOConn();
56
// 创建记录集对象
57
m_pRecordset.CreateInstance(__uuidof(Recordset));
58
// 取得表中的记录
59
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
60
}
61
// 捕捉异常
62
catch(_com_error e)
63
{
64
// 显示错误信息
65
AfxMessageBox(e.Description());
66
}
67
// 返回记录集
68
return m_pRecordset;
69
}
70
71
// 执行SQL语句,Insert Update _variant_t
72
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
73

{
74
// _variant_t RecordsAffected;
75
try
76
{
77
// 是否已经连接数据库
78
if(m_pConnection == NULL)
79
OnInitADOConn();
80
// Connection对象的Execute方法:(_bstr_t CommandText,
81
// VARIANT * RecordsAffected, long Options )
82
// 其中CommandText是命令字串,通常是SQL命令。
83
// 参数RecordsAffected是操作完成后所影响的行数,
84
// 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名
85
// adCmdProc-存储过程;adCmdUnknown-未知
86
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
87
return true;
88
}
89
catch(_com_error e)
90
{
91
AfxMessageBox(e.Description());
92
return false;
93
}
94
}
95
96
void ADOConn::ExitConnect()
97

{
98
// 关闭记录集和连接
99
if (m_pRecordset != NULL)
100
m_pRecordset->Close();
101
m_pConnection->Close();
102
// 释放环境
103
::CoUninitialize();
104
}
建立本类的一个对象 然后就可访问mysql数据库了:
1
ADOConn m_ADO;
2
m_ADO.OnInitADOConn();
3
4
//设置SELECT语句
5
_bstr_t vSQL;
6
vSQL = "SELECT * FROM test WHERE id = 1";
7
//执行SELETE语句
8
_RecordsetPtr m_pRecordset;
9
m_pRecordset = m_ADO.GetRecordSet(vSQL);
10
11
CString name0;
12
//返回各列的值
13
if (!m_pRecordset->adoEOF)
14
{
15
16
name0 = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("name");
17
}
18
//断开与数据库的连接
19
m_ADO.ExitConnect();
20
21
MessageBox(name0, L"id = 1", 0);
运行结果: