MFC – ODBC操作数据库(MYSQL,SQL Server,ACCESS)

最近做了一个项目要用到MySQL,SQL Server,ACCESS数据,都是使用ODBC的方式进行数据库操作。

 
JDataBase.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")
class JDataBase
{
public:
    JDataBase();
    ~JDataBase();

    void SetParam(DatabaseInfo DBInfo, CString DNS = "MYSQL");
    void SetParam(CString Server, CString User, CString Passwd, CString DBName,  CString DNS = "MYSQL");
    BOOL OnInitADOConn();
    //执行查询
    _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
    //执行SQL语句
    BOOL ExecuteSQL(_bstr_t bstrSQL);
    //断开数据库连接
    void ExitConnect();

    // 变量
    //添加一个指向Recordset对象的指针
    _RecordsetPtr m_pRecordset;
    //添加一个指向Connection对象的指针
    _ConnectionPtr m_pConnection;

private:
    // 变量
    CString Server;
    CString LogName;
    CString Passwd;
    CString DBName;
    // Access:MS Access Database
    // Mysql:MYSQL
    // SQL Server:
    CString DNS;
    // 函数
    BOOL IsInfoEmpty();
};

 
JDataBase.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "StdAfx.h"
#include "JDataBase.h"

JDataBase::JDataBase()
{
    Server = "";
    LogName = "";
    Passwd = "";
    DBName = "";
    DNS = "MYSQL";
}


JDataBase::~JDataBase()
{
}

void JDataBase::SetParam(DatabaseInfo DBInfo, CString DNS)
{
    this->Server = DBInfo.Server;
    this->LogName = DBInfo.UserName;
    this->Passwd = DBInfo.Passwd;
    this->DBName = DBInfo.Database;
    this->DNS = DNS;
}

void JDataBase::SetParam(CString Server, CString User, CString Passwd, CString DBName, CString DNS )
{
    this->Server = Server;
    this->LogName = User;
    this->Passwd = Passwd;
    this->DBName = DBName;
    this->DNS = DNS;
}

BOOL JDataBase::OnInitADOConn()
{
    CString SQL;
    if (DNS == "ACCESS")
        SQL = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBName;
    else {
        if (!IsInfoEmpty())
            return FALSE;
        SQL = "DSN=" + DNS + ";Server=" + Server + ";Database=" + this->DBName + ";Uid=" + this->LogName + ";" + "Pwd=" + this->Passwd + ";";
    }
    try {
        //创建connection对象
        m_pConnection.CreateInstance("ADODB.Connection");
        //设置连接字符串
        _bstr_t strConnect = _T(SQL);
        m_pConnection->ConnectionTimeout = 3;
        //SERVER和UID,PWD的设置根据实际情况来设置
        m_pConnection->Open(strConnect, "", "", adModeUnknown);
    }
    catch (_com_error e) {
        //显示错误信息
        CString   err;
        err.Format("%s", (LPCTSTR)e.Description());
        //MessageBox(NULL,err,"提示", MB_ICONEXCLAMATION );
        return FALSE;
    }
    return TRUE;
}

BOOL JDataBase::IsInfoEmpty()
{
    if (Server.IsEmpty() || LogName.IsEmpty() || Passwd.IsEmpty() || DNS.IsEmpty())
        return FALSE;
    return TRUE;
}

_RecordsetPtr& JDataBase::GetRecordSet(_bstr_t bstrSQL)
{
    try {
        //创建记录集对象
        m_pRecordset.CreateInstance(__uuidof(Recordset));
        //取得表中的记录
        m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(),
            adOpenDynamic, adLockOptimistic, adCmdText);
    }
    catch (_com_error e) {
        e.Description();
    }
    //返回记录集
    return m_pRecordset;
}

BOOL JDataBase::ExecuteSQL(_bstr_t bstrSQL)
{
    _variant_t RecordsAffected;
    try {
        m_pConnection->Execute(bstrSQL, NULL, adCmdText);
        return TRUE;
    }
    catch (_com_error e) {
        e.Description();
        return FALSE;
    }
}

void JDataBase::ExitConnect()
{
    try {
        if (m_pRecordset != NULL)
            m_pRecordset->Close();
        m_pConnection->Close();
    }
    catch (...) {
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注