-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdodbHelper.cls
546 lines (466 loc) · 16.3 KB
/
AdodbHelper.cls
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "AdodbHelper"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'////////说明////////
'使用前,请先引用:Microsoft ActiveX Data Objects 2.5 Library
'引用方式:工程→引用→Microsoft ActiveX Data Objects 2.5 Library
'最后修改日期:2015年1月15日22:16:43
'2015年01月15日22:19:23 全部重新格式化
'2016年02月09日15:33:17 更新部分Adodb缺失
'成员变量:连接对象
Private m_Conn As ADODB.Connection
'成员变量:SQL命令对象
Private m_Command As ADODB.Command
'成员变量:连接字符串
Private m_ConnString As String
'成员变量:数据库文件路径
Private m_FilePath As String
'成员变量:自身对象
Private m_Me As New AdodbHelper
'规定:每个函数执行完成后,都必须清空m_Command,并且创建一个空的对象
'【类初始化、释放】
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : Class_Initialize
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:56
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Private Sub Class_Initialize()
End Sub
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : Class_Terminate
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:56
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Private Sub Class_Terminate()
Set m_Conn = Nothing
Set m_Command = Nothing
End Sub
'【类属性】
'读取:数据库连接字符串
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : ConnectionString
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:56
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Public Property Get ConnectionString() As String
ConnectionString = m_ConnString
End Property
'设置:连接字符串
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : ConnectionString
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:56
'
' Parameters : vNewValue (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Property Let ConnectionString(ByVal vNewValue As String)
m_ConnString = vNewValue
End Property
'类是否准备好,是否可以执行SQL
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : IsReady
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:56
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Public Property Get IsReady() As Boolean
IsReady = IIf(Len(ConnectionString) > 0, True, False)
End Property
'【类方法】
'执行查询,返回离线记录集
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : ExecQuery
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : SqlStr (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecQuery(ByVal SqlStr As String) As ADODB.Recordset
Dim tempRes As New ADODB.Recordset
Set m_Command = New ADODB.Command
Call OpenConn
m_Command.ActiveConnection = m_Conn
m_Command.CommandText = SqlStr
Set tempRes = m_Command.Execute()
tempRes.ActiveConnection = Nothing
Call CloseConn
Set ExecQuery = tempRes
Set m_Command = Nothing
End Function
'执行参数化查询,返回离线记录集
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : ExecParamQuery
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : SqlStr (String)
' Params() (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecParamQuery(ByVal SqlStr As String, _
ParamArray Params()) As ADODB.Recordset
Dim tempRes As New ADODB.Recordset
Dim i As Long
Dim paramArr As Variant
Set m_Command = New ADODB.Command
'打开连接
Call OpenConn
m_Command.ActiveConnection = m_Conn
m_Command.CommandText = SqlStr
m_Command.CommandType = adCmdText
'过滤嵌套情况
paramArr = Params
If VarType(Params(0)) = 8204 Then
paramArr = Params(0)
End If
'设置参数
With m_Command
For Each param In paramArr
Dim Para As ADODB.Parameter
Set Para = .CreateParameter(CStr(i), GetVarType(param), adParamInput, LenB(param))
Para.Value = param
.Parameters.Append Para
Next
End With
'获取执行后记录集
Set tempRes = m_Command.Execute()
'与数据库连接脱钩
tempRes.ActiveConnection = Nothing
'关闭数据库连接
Call CloseConn
'返回数据集对象引用
Set ExecParamQuery = tempRes
'清空命令对象
Set m_Command = Nothing
End Function
'执行查询,返回影响行数
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : ExecNonQuery
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : SqlStr (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecNonQuery(ByVal SqlStr As String) As Long
'定义影响行数变量
Dim affectedRows As Long
'创建绑定Command对象
Set m_Command = New ADODB.Command
'打开连接
Call OpenConn
'绑定Command到数据库连接
m_Command.ActiveConnection = m_Conn
'设置SQL语句
m_Command.CommandText = SqlStr
'设置SQL类型
m_Command.CommandType = adCmdText
'获取执行后影响行数
m_Command.Execute affectedRows
'关闭数据库连接
Call CloseConn
'清空命令对象
Set m_Command = Nothing
'返回影响行数
ExecNonQuery = affectedRows
End Function
'执行参数化查询,返回影响行数
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : ExecParamNonQuery
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : SqlStr (String)
' Params() (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecParamNonQuery(ByVal SqlStr As String, ParamArray Params()) As Long
Dim i As Long
Dim affectedRows As Long
Dim paramArr As Variant
Set m_Command = New ADODB.Command
'打开连接
Call OpenConn
m_Command.ActiveConnection = m_Conn
m_Command.CommandText = SqlStr
m_Command.CommandType = adCmdText
'过滤嵌套情况
paramArr = Params
If VarType(Params(0)) = 8204 Then
paramArr = Params(0)
End If
'设置参数
With m_Command
For Each param In paramArr
Dim Para As ADODB.Parameter
Set Para = .CreateParameter(CStr(i), GetVarType(param), adParamInput, LenB(param))
Para.Value = param
.Parameters.Append Para
Next
End With
'获取执行后记录集
m_Command.Execute affectedRows
'关闭数据库连接
Call CloseConn
'清空命令对象
Set m_Command = Nothing
'返回影响行数
ExecParamNonQuery = affectedRows
End Function
'根据文件路径设置连接字符串
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : SetConnToFile
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : FilePath (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Sub SetConnToFile(ByVal FilePath As String)
m_ConnString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & ";"
End Sub
'释放离线记录集
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : ReleaseRecordset
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : dbRes (ADODB.Recordset)
'--------------------------------------------------------------------------------
'</CSCM>
Public Sub ReleaseRecordset(ByRef dbRes As ADODB.Recordset)
Set dbRes = Nothing
End Sub
'将制定表数据从一个数据库文件插入到另一个数据库文件中
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : TransData
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : SourceDb (String)
' DestDb (String)
' tableNames() (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Public Sub TransData(ByVal SourceDb As String, _
ByVal DestDb As String, _
ParamArray tableNames())
Dim TableName As Variant
m_Me.SetConnToFile SourceDb
If UBound(tableNames) <= 0 Then
Exit Sub
End If
For Each TableName In tableNames
m_Me.ExecQuery "select * into [" & DestDb & "]." & TableName & " from " & TableName
Next
End Sub
'从打开数据库文件,返回一个打开的数据库连接
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : DbConnFromFile
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : FilePath (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function DbConnFromFile(ByVal FilePath As String) As ADODB.Connection
Dim tmpConn As New ADODB.Connection
tmpConn.CursorLocation = adUseClient
tmpConn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & ";"
Set DbConnFromFile = tmpConn
End Function
'执行参数化查询,返回首行,首列值
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : ExecParamQueryScalar
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : SqlStr (String)
' Params() (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecParamQueryScalar(ByVal SqlStr As String, _
ParamArray Params()) As Variant
Dim varResult As Variant
Dim tmpRes As ADODB.Recordset
Set tmpRes = ExecParamQuery(SqlStr, Params)
If tmpRes.RecordCount <= 0 Then
Set ExecParamQueryScalar = Nothing
Else
ExecParamQueryScalar = tmpRes.fields(0).Value
End If
ReleaseRecordset tmpRes
End Function
'执行查询,返回首行,首列值
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : ExecQueryScalar
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : SqlStr (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecQueryScalar(ByVal SqlStr As String) As Variant
Dim varResult As Variant
Dim tmpRes As ADODB.Recordset
Set tmpRes = ExecQuery(SqlStr)
If tmpRes.RecordCount <= 0 Then
Set ExecQueryScalar = Nothing
Else
ExecQueryScalar = tmpRes.fields(0).Value
End If
ReleaseRecordset tmpRes
End Function
'【内部方法】
'打开数据库连接
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : OpenConn
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Private Sub OpenConn()
Set m_Conn = New ADODB.Connection
m_Conn.CursorLocation = adUseClient
m_Conn.Open ConnectionString
End Sub
'关闭数据库连接
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : CloseConn
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Private Sub CloseConn()
m_Conn.Close
Set m_Conn = Nothing
End Sub
'返回VB变量类型返回数据库参数的类型枚举值
'<CSCM>
'--------------------------------------------------------------------------------
' Project : 类库构建项目
' Procedure : GetVarType
' Description: [type_description_here]
' Created by : Project Administrator
' Machine : MYSOFT-SUNR01
' Date-Time : 1-18-2015-18:19:55
'
' Parameters : Value (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Private Function GetVarType(ByRef Value As Variant) As ADODB.DataTypeEnum
Dim k As New ADODB.Command
Select Case VarType(Value)
Case VbVarType.vbString
GetVarType = ADODB.DataTypeEnum.adVarWChar
Case VbVarType.vbInteger
GetVarType = ADODB.DataTypeEnum.adSmallInt
Case VbVarType.vbBoolean
GetVarType = ADODB.DataTypeEnum.adBoolean
Case VbVarType.vbCurrency
GetVarType = ADODB.DataTypeEnum.adCurrency
Case VbVarType.vbDate
GetVarType = ADODB.DataTypeEnum.adDate
Case 8209
GetVarType = ADODB.DataTypeEnum.adLongVarBinary
Case Else
GetVarType = ADODB.DataTypeEnum.adVariant
End Select
End Function