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
13class ADOConn
14 {
15// 定义变量
16public:
17 //添加一个指向Connection对象的指针:
18 _ConnectionPtr m_pConnection;
19 //添加一个指向Recordset对象的指针:
20 _RecordsetPtr m_pRecordset;
21// 定义方法
22public:
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
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
13class ADOConn
14 {
15// 定义变量
16public:
17 //添加一个指向Connection对象的指针:
18 _ConnectionPtr m_pConnection;
19 //添加一个指向Recordset对象的指针:
20 _RecordsetPtr m_pRecordset;
21// 定义方法
22public:
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
10static 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
23ADOConn::~ ADOConn()
24 {
25
26}
27
28// 初始化—连接数据库
29void 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
96void ADOConn::ExitConnect()
97 {
98 // 关闭记录集和连接
99 if (m_pRecordset != NULL)
100 m_pRecordset->Close();
101 m_pConnection->Close();
102 // 释放环境
103 ::CoUninitialize();
104}
2 //
3 /**/ //
4
5#include "stdafx.h"
6#include "ADOConn.h"
7
8 #ifdef _DEBUG
9#undef THIS_FILE
10static 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
23ADOConn::~ ADOConn()
24 {
25
26}
27
28// 初始化—连接数据库
29void 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
96void 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);
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);