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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "StdAfx.h"

#include "ADOConnect.h"

//
// StdAfx.h中加入 #import "E:\vs2008\BuildNcProgram\BuildNcProgram\dll\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF") //导入ADO动态连接库
//
//
CAdoConnect::CAdoConnect(const CString& szConnectStr)
:m_szConnectStr(szConnectStr)
, m_szServer(_T(""))
, m_szDATABASE(_T(""))
, m_szUID(_T(""))
, m_szPWD(_T(""))
, m_nPort(1433)
{
::CoInitialize(NULL); //初始化OLE/COM库环境
}
CAdoConnect::CAdoConnect(const CString& Server
, const CString& DATABASE
, const CString& UID
, const CString& PWD
, const int nPort
)
: m_szServer(Server)
, m_szDATABASE(DATABASE)
, m_szUID(UID)
, m_szPWD(PWD)
, m_szConnectStr(_T(""))
, m_nPort(nPort)
{
m_szConnectStr.Empty();
::CoInitialize(NULL); //初始化OLE/COM库环境
}


CAdoConnect::~CAdoConnect(void)
{
//关闭记录集和连接
if (m_pRecordset != NULL)
{
if (m_pRecordset->GetState() == adStateOpen)
{
m_pRecordset->Close();
}
if (m_pRecordset)
{
m_pRecordset.Release();
m_pRecordset = NULL;
}
}


if (m_pConnection != NULL)
{
if (m_pConnection->GetState() == adStateOpen)
{
m_pConnection->Close();
}
m_pConnection.Release();
m_pConnection = NULL;
}

::CoUninitialize(); //释放程序占用的COM资源
}

// 连接数据库
BOOL CAdoConnect::OnInitAdoConnection(void)
{
try
{
HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection 对象
if (SUCCEEDED(hr))
{
m_pConnection->CursorLocation = adUseServer;

// "Provider=SQLOLEDB.1;Server=数据库IP地址;UID=sa;PWD=你的sa登陆密码;DATABASE=数据库名称;";
//_bstr_t strConnect = "driver={SQL Server};Server=192.168.0.117;DATABASE=TEST;UID=uid;PWD=passward;";
CString szStr;
if (m_szConnectStr.IsEmpty())
{
szStr.Format(_T("driver={SQL Server};Server=%s,%d;DATABASE=%s;UID=%s;PWD=%s;")
, m_szServer, m_nPort, m_szDATABASE, m_szUID, m_szPWD);
}
else
{
szStr = m_szConnectStr;// = "driver={SQL Server};Server=localhost;DATABASE=EOSDB;UID=sa;PWD=12345;";
}
//szStr = m_szConnectStr = "driver={SQL Server};Server=127.0.0.1,1433;DATABASE=EOSDB;UID=sa;PWD=12345;";

_bstr_t strConnect = szStr;
// "driver={SQL Server};Server=数据库IP地址;DATABASE=数据库名称;UID=sa;PWD=你的sa登陆密码;";
HRESULT hr = m_pConnection->Open(strConnect, "", "", adModeUnknown);
if (SUCCEEDED(hr))
{
return TRUE;
}
else
{
AfxMessageBox("建立数据库连接失败!");
return FALSE;
}
}
}
catch (_com_error e)
{
AfxMessageBox(e.Description());//显示错误信息
return FALSE;
}
catch (...)
{
return FALSE;
}

return TRUE;
}

// 执行Insert UpDate
BOOL CAdoConnect::ExecuteSQL(_bstr_t bstrSQL, long lOptions)
{
try
{
//是否已经连接到数据库
if (m_pConnection == NULL)
{
OnInitAdoConnection();
}
m_pRecordset = m_pConnection->Execute(bstrSQL, NULL, lOptions);
return TRUE;
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
return FALSE;
}
catch (...)
{
return FALSE;
}
}


//执行查询
_RecordsetPtr& CAdoConnect::GetRecordSet(_bstr_t bstrSQL)
{
try
{
//连接数据库 如果Connection对象为空则重新连接数据库
if (m_pConnection == NULL)
{
OnInitAdoConnection();
}
//创建记录集对象
HRESULT hr = m_pRecordset.CreateInstance(__uuidof(Recordset));
//取得表中的记录
if (SUCCEEDED(hr))
{
m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
}
catch (_com_error e)
{
//异常 显示错误信息
AfxMessageBox(e.Description());
}
return m_pRecordset;
}#pragma once
//#import "E:\vs2008\BuildNcProgram\BuildNcProgram\dll\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF") //导入ADO动态连接库
#import "c:\Program Files\Common Files\System\ado\msado15.dll"no_namespace \
rename("EOF", "adoEOF")rename("BOF", "adoBOF")
class CAdoConnect
{
private:
CString m_szConnectStr;
CString m_szServer;
CString m_szDATABASE;
CString m_szUID;
CString m_szPWD;
int m_nPort;
private:
_ConnectionPtr m_pConnection; // 连接对象指针
_RecordsetPtr m_pRecordset; // 记录集对象指针

public:
CAdoConnect(const CString& szConnectStr);
CAdoConnect(const CString& Server
, const CString& DATABASE
, const CString& UID
, const CString& PWD
, const int nPort = 1433
);
virtual~CAdoConnect(void);

public:
// 连接数据库
BOOL OnInitAdoConnection(void);
// 执行Insert UpDate
BOOL ExecuteSQL(_bstr_t bstrSQL, long lOptions = adCmdText);
// 执行查询得到记录集
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);

// 操作符重载
public:
_RecordsetPtr& operator->()
{
return m_pRecordset;
}
};