-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDB_table.cpp
137 lines (120 loc) · 3.92 KB
/
DB_table.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
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
// DB_table.cpp : 实现文件
//
#include "stdafx.h"
#include "DB_table.h"
#include "afxdialogex.h"
#include <fstream>
using namespace std;
UINT message(void* pp){
MessageBox(0, (const char*)(pp), "温馨提示", 0);
return 0;
}
// DB_table 对话框
IMPLEMENT_DYNAMIC(DB_table, CDialog)
DB_table::DB_table(CWnd* pParent /*=NULL*/)
: m_dbName("source.dbf"), DBName("Provider=Microsoft.JET.OLEDB.4.0;Data source=" + m_dbName), LogDir("Log\\dbError-log.txt")
{
memset(source,0,sizeof(source));
strcpy(source,"ImgSource");
table[0].tableName = "fileList";
table[0].tvalueName[0] = "md5";
table[0].tvalueName[1] = "srcName";
table[0].tvalueName[2] = "fileName";
}
CString DB_table::findData(const char* s1,int re_i){
ADODB::_ConnectionPtr m_pConnection;
_variant_t RecordsAffected;
ADODB::_RecordsetPtr m_pRecordset;
try{
m_pConnection.CreateInstance(__uuidof(ADODB::Connection));
m_pConnection->Open(DBName, "", "", ADODB::adModeUnknown);
}catch(_com_error e){
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
#ifdef _DUBUG
AfxBeginThread(message, (void*)(LPCTSTR)errormessage);
#else
ofstream DBLog(LogDir, ios::out | ios::app);
DBLog << __DATE__ <<" " __TIME__ <<"\t"<<__FILE__<<"--line:"<<__LINE__<<endl<<errormessage<<"\n\n";
DBLog.close();
#endif
if (m_pConnection && m_pConnection->State)
m_pConnection->Close();
return "";//exit(-1);
}
try{
_bstr_t strCmd="select * from "+table[0].tableName+" where "+table[0].tvalueName[0]+"='"+s1+"'";
m_pRecordset=m_pConnection->Execute(strCmd,&RecordsAffected,ADODB::adCmdText);
if (!m_pRecordset->adoEOF && re_i < TValueNums && re_i >= 0)
return (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect(_variant_t((long)re_i));
else
return "";
}catch(_com_error &e){
if (m_pRecordset)
m_pRecordset->Close();
if (m_pConnection && m_pConnection->State)
m_pConnection->Close();
CString errormessage;
errormessage.Format("搜索失败!\r\n错误信息:%s",e.ErrorMessage());
#ifdef _DUBUG
AfxBeginThread(message, (void*)(LPCTSTR)errormessage);
#else
ofstream DBLog(LogDir, ios::out | ios::app);
DBLog << __DATE__ << " " __TIME__ << "\t" << __FILE__ << "--line:" << __LINE__ << endl << errormessage << "\n\n";
DBLog.close();
#endif
return "";//exit(-1);
}
if (m_pConnection && m_pConnection->State)
m_pConnection->Close();
}
void DB_table::addRow(const char* s1, CString s2, CString s3){
ADODB::_ConnectionPtr m_pConnection;
_variant_t RecordsAffected;
try{
m_pConnection.CreateInstance(__uuidof(ADODB::Connection));
m_pConnection->Open(DBName,"","",ADODB::adModeUnknown);
}catch(_com_error e){
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
#ifdef _DUBUG
AfxBeginThread(message, (void*)(LPCTSTR)errormessage);
#else
ofstream DBLog(LogDir, ios::out | ios::app);
DBLog << __DATE__ << " " __TIME__ << "\t" << __FILE__ << "--line:" << __LINE__ << endl << errormessage << "\n\n";
DBLog.close();
#endif
if (m_pConnection && m_pConnection->State)
m_pConnection->Close();
return;//exit(-1);
}
try{
_bstr_t strCmd="INSERT INTO "+table[0].tableName+" values ('"+s1+"','"+s2+"','"+s3+"');";
m_pConnection->Execute(strCmd,&RecordsAffected,ADODB::adCmdText);
}catch(_com_error &e){
CString errormessage;
errormessage.Format("录入失败!\r\n错误信息:%s",e.ErrorMessage());
#ifdef _DUBUG
AfxBeginThread(message, (void*)(LPCTSTR)errormessage);
#else
ofstream DBLog(LogDir, ios::out | ios::app);
DBLog << __DATE__ << " " __TIME__ << "\t" << __FILE__ << "--line:" << __LINE__ << endl << errormessage << "\n\n";
DBLog.close();
#endif
if (m_pConnection && m_pConnection->State)
m_pConnection->Close();
return;//exit(-1);
}
if (m_pConnection && m_pConnection->State)
m_pConnection->Close();
}
DB_table::~DB_table()
{
}
void DB_table::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(DB_table, CDialog)
END_MESSAGE_MAP()
// DB_table 消息处理程序