forked from hsluoyz/PenetrationTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataConnection.cpp
100 lines (80 loc) · 1.94 KB
/
DataConnection.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
#include "DataConnection.h"
//数据库操作封装类,采用ADO技术实现
//Constructor
DataConnection::DataConnection()
{
//do nothing
}
//Destructor
DataConnection::~DataConnection()
{
//do nothing
}
//Initialize connectiong to database
void DataConnection::OnInitADOConn()
{
::CoInitialize(NULL);
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");
if(m_pConnection->State) m_pConnection->Close();
if( SUCCEEDED(hr) )
{
m_pConnection->ConnectionTimeout = 5;
_bstr_t strConnect = "Provider=SQLOLEDB;Server=127.0.0.1;Database=tool;uid=sa;pwd="; //连接本机SQL Server数据库软件中的tool数据库
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
}
catch(_com_error e) //连接失败,比如SQL Server数据库没装
{
MessageBox(NULL,e.Description(),"Information",MB_ICONINFORMATION | MB_OK);
}
}
//Get record of database
bool DataConnection::GetRecordSet(_RecordsetPtr &m_pRecordset , _bstr_t bstrSQL)
//第一个参数是引用类型,也就是传出参数,可看做函数的返回值,第二个参数是SQL语句,是输入。
{
try
{
if(m_pConnection==NULL) //初始化连接
OnInitADOConn();
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pConnection->CursorLocation = adUseClient;
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
return true;
}
catch(_com_error e) //失败
{
MessageBox(NULL,e.Description(),"提示",MB_ICONINFORMATION | MB_OK);
return false;
}
}
BOOL DataConnection::ExecuteSQL(_bstr_t bstrSQL)
{
try
{
if(m_pConnection == NULL) //初始化连接
{
OnInitADOConn();
}
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e) //失败
{
MessageBox(NULL,e.Description(),"Information",MB_ICONINFORMATION | MB_OK);
return false;
}
}
//Close the connection
//关闭记录集和连接
void DataConnection::ExitConnect()
{
if (m_pRecordset != NULL)
{
m_pRecordset->Close();
}
m_pConnection->Close();
::CoUninitialize();
}