Repository navigation
Expand file tree
/
Copy pathXEIM_Database.cpp
More file actions
362 lines (301 loc) · 7.73 KB
/
Copy pathXEIM_Database.cpp
File metadata and controls
362 lines (301 loc) · 7.73 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "StdAfx.h"
#include "XEIM_Database.h"
// Download by http://www.codefans.net
static XEIM_Database dbOnlyOne;
bool XEIM_Database::m_bStart = false;
XEIM_Database::XEIM_Database()
{
m_pConn = NULL;
m_pRs = NULL;
m_strLastError = "";
OleInitialize(NULL);
/* ConnectSQL(m_config.m_szServer.c_str(),
m_config.m_szDatabase.c_str(),
m_config.m_szUserName.c_str(),
m_config.m_szPassword.c_str());*/
// ConnectAccess("xeimDatabase.mdb");
}
XEIM_Database::~XEIM_Database()
{
OleUninitialize();
}
// 连接 MS SQL 类型数据库;
BOOL XEIM_Database::ConnectSQL(LPCTSTR server, LPCTSTR db, LPCTSTR user, LPCTSTR password)
{
/* MS SQL 的连接语句 */
CString strConn;
strConn.Format("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=%s;Password=%s;Initial Catalog=%s;Data Source=%s",
user,password,db,server);
HRESULT hr;
try {
// m_pConn 在构造函数里,必须初始化为 NULL
hr = m_pConn.CreateInstance(__uuidof(Connection));
// 打开数据库 strConn
m_pConn->CursorLocation=adUseClient;
hr = m_pConn->Open((LPCTSTR)strConn, "", "", adModeUnknown);
}
catch(_com_error e) {
m_strLastError = e.Description();
}
if (! m_pConn->State)
{
m_strLastError = "数据库打开失败!";
return FALSE;
}
m_pRs.CreateInstance(__uuidof(Recordset));
return TRUE;
}
// 查询某条记录是否存在;
BOOL XEIM_Database::RecordExists(LPCTSTR szSQL)
{
HRESULT hr;
BOOL bRet = FALSE;
try {
hr = m_pRs->Open(szSQL,
m_pConn.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
// if (SUCCEEDED(hr))
// AddErrorInfo("rs failed.");
// 如果数据库是空白的
if (! m_pRs->adoEOF)
bRet = TRUE;
}
catch (_com_error e)
{
AfxMessageBox(e.ErrorMessage());
m_pRs.Release();
return FALSE;
}
m_pRs->Close();
// m_pRs.Release();
return bRet;
}
// 执行一条 SQL 语句;
BOOL XEIM_Database::ExecuteCommand(LPCTSTR szSQL)
{
try
{
_variant_t vAffected;
m_pConn->CursorLocation = adUseClient;
m_pConn->Execute(szSQL, &vAffected, adExecuteNoRecords);
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
return FALSE;
}
return TRUE;
}
_RecordsetPtr &XEIM_Database::GetChatText(const char *szIdone, const char *szIdtwo, const char *szLastTime)
{
CString strReturn;
CString strSQL;
CTime tmNow = CTime::GetCurrentTime();
ASSERT(NULL != szLastTime);
if (0 == strcmp(szIdone, szIdtwo))
{
char *sql = "select [%s], [%s], [%s], [%s], [%s] from [%s]"
"where ([%s]='%s' and [%s]='%s') and ([%s] between #%s# and #%s#);";
strSQL.Format(sql, "sender", "receiver", "message", "senddate", "recvdate",
"freeeim", "sender", szIdone, "receiver", szIdtwo,
"senddate", szLastTime, tmNow.Format(_T("%Y-%m-%d %H:%M:%S")));
}
else
{
char *sql = "select [%s], [%s], [%s], [%s], [%s] from [%s]"
"where (([%s]='%s' and [%s]='%s') "
"or ([%s]='%s' and [%s]='%s')) and ([%s] between #%s# and #%s#);";
strSQL.Format(sql, "sender", "receiver", "message", "senddate", "recvdate",
"freeeim", "sender", szIdone, "receiver", szIdtwo,
"sender", szIdtwo, "receiver", szIdone,
"senddate", szLastTime, tmNow.Format(_T("%Y-%m-%d %H:%M:%S")));
}
_variant_t var;
try
{
HRESULT hr;
hr = m_pRs->Open((LPCTSTR)strSQL, m_pConn.GetInterfacePtr(), adOpenDynamic,
adLockOptimistic, adCmdText);
return m_pRs;
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
return m_pRs;
}
return m_pRs;
}
_RecordsetPtr &XEIM_Database::GetChatTextByDay(const char *szIdone, const char *szIdtwo, const char *szLastTime)
{
CString strReturn;
CString strSQL;
CTime tmNow = CTime::GetCurrentTime();
CString strNow = tmNow.Format(_T("%Y-%m-%d %H:%M:%S"));
ASSERT(NULL != szLastTime);
CString daytime_begin = CString(szLastTime) + " 00:01:01";
CString daytime_end = CString(szLastTime) + " 23:59:59";
if (0 == strcmp(szIdone, szIdtwo))
{
char *sql = "select [%s], [%s], [%s], [%s], [%s] from [%s]"
"where ([%s]='%s' and [%s]='%s') and ([%s] between #%s# and #%s#);";
strSQL.Format(sql, "sender", "receiver", "message", "senddate", "recvdate",
"freeeim", "sender", szIdone, "receiver", szIdtwo,
"senddate", daytime_begin, daytime_end);
}
else
{
char *sql = "select [%s], [%s], [%s], [%s], [%s] from [%s]"
"where (([%s]='%s' and [%s]='%s') "
"or ([%s]='%s' and [%s]='%s')) and ([%s] between #%s# and #%s#);";
strSQL.Format(sql, "sender", "receiver", "message", "senddate", "recvdate",
"freeeim", "sender", szIdone, "receiver", szIdtwo,
"sender", szIdtwo, "receiver", szIdone,
"senddate", daytime_begin, daytime_end);
}
_variant_t var;
try
{
HRESULT hr;
hr = m_pRs->Open((LPCTSTR)strSQL, m_pConn.GetInterfacePtr(), adOpenDynamic,
adLockOptimistic, adCmdText);
return m_pRs;
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
return m_pRs;
}
return m_pRs;
}
// 通过条件获取唯一 ID
DWORD XEIM_Database::GetIdByCondition(LPCTSTR szCondition)
{
/* HRESULT hr;
DWORD dwRet = 0;
try {
hr = m_pRs->Open(szSQL,
m_pConn.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
// if (SUCCEEDED(hr))
// AddErrorInfo("rs failed.");
// 如果数据库是空白的
if (! m_pRs->adoEOF)
{
var[1] = pRs->GetCollect(_variant_t());
if (var[1].vt != VT_NULL)
{
pData->SetData((LPCTSTR)_bstr_t(var[1]));
}
}
pRs->Close();
}
catch (_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
*/
return -1;
}
// 功能:按条件读取某个字段的内容;
// 更细日期:2009年11月17日
void XEIM_Database::GetField(const char* szTable, const char *szField, const char *szCondition, char *szData)
{
HRESULT hr;
CString strSQL;
strSQL.Format("SELECT %s FROM %s WHERE %s",szField, szTable, szCondition);
_variant_t var;
try {
hr = m_pRs->Open((LPCTSTR)strSQL,
m_pConn.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
// if (SUCCEEDED(hr))
// AddErrorInfo("rs failed.");
// 如果数据库不是空白的
if (! m_pRs->adoEOF)
{
// 获取数据
var = m_pRs->GetCollect(_variant_t(szField));
if (var.vt != VT_NULL)
{
strcpy(szData, (LPCTSTR)_bstr_t(var));
}
}
else
{
szData = NULL;
}
}
catch (_com_error e) {
AfxMessageBox(e.ErrorMessage());
m_pRs.Release();
}
m_pRs->Close();
}
// 连接 MS ACCESS 数据库;
BOOL XEIM_Database::ConnectAccess(const char *szMDB)
{
CString strConn;
/* MS ACCESS 的连接语句 */
strConn.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s", szMDB);
HRESULT hr;
try {
hr = m_pConn.CreateInstance(__uuidof(Connection));
// 打开数据库 strConn
m_pConn->CursorLocation=adUseClient;
hr = m_pConn->Open((LPCTSTR)strConn, "", "", adModeUnknown);
}
catch(_com_error e) {
m_strLastError = (LPCTSTR)e.Description();
}
if (! m_pConn->State)
{
return FALSE;
}
m_pRs.CreateInstance(__uuidof(Recordset));
return TRUE;
}
BOOL XEIM_Database::IsConnected()
{
return m_pConn->State == adStateOpen;
}
BOOL XEIM_Database::SetField(LPCTSTR szTable, LPCTSTR szField, LPCTSTR szData, LPCTSTR szFieldCondition, LPCTSTR szDataCondition)
{
BOOL bRet = FALSE;
if (m_pConn->State != adStateOpen)
{
m_strLastError = "XEIM_Database::UpdateUserStatus,数据无法打开!";
return bRet;
}
if (szTable == NULL
|| szField == NULL
|| szData == NULL)
{
m_strLastError = "szTable或szField,szData参数为NULL。";
return bRet;
}
CString strSQL;
strSQL.Format("UPDATE [%s] SET [%s]='%s' WHERE [%s]=%s", szTable, szField, szData, szFieldCondition, szDataCondition);
// AfxMessageBox(strSQL);
try
{
_variant_t vAffected;
m_pConn->CursorLocation = adUseClient;
m_pConn->Execute((LPCTSTR)strSQL, &vAffected, adExecuteNoRecords);
bRet = TRUE;
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
return FALSE;
}
return bRet;
}
void XEIM_Database::StartDB(const char *szMDB)
{
if(false == m_bStart)
{
m_bStart = true;
dbOnlyOne.ConnectAccess(szMDB);
}
}
XEIM_Database &XEIM_Database::GetDB()
{
return dbOnlyOne;
}