-
Notifications
You must be signed in to change notification settings - Fork 1
/
DbCreateHelper.cls
183 lines (158 loc) · 5.39 KB
/
DbCreateHelper.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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "DbCreateHelper"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'//////////////////////////////////////////////////////////////////////////////
'@@summary:请引用“Microsoft ADO Ext. 6.0 for DDL and Security”
'@@require
'@@reference
'@@license
'@@author
'@@create
'@@modify
'//////////////////////////////////////////////////////////////////////////////
'//////////////////////////////////////////////////////////////////////////////
'//
'// 私有声明
'//
'//////////////////////////////////////////////////////////////////////////////
'------------------------------------------------------------------------------
' 私有变量
'------------------------------------------------------------------------------
Private m_ConnStr As String
Private m_CT As New ADOX.Catalog
'//////////////////////////////////////////////////////////////////////////////
'//
'// 类
'//
'//////////////////////////////////////////////////////////////////////////////
'------------------------------------------------------------------------------
' 初始化
'------------------------------------------------------------------------------
Private Sub Class_Initialize()
End Sub
'------------------------------------------------------------------------------
' 销毁
'------------------------------------------------------------------------------
Private Sub Class_Terminate()
Set m_CT = Nothing
End Sub
'//////////////////////////////////////////////////////////////////////////////
'//
'// 私有方法
'//
'//////////////////////////////////////////////////////////////////////////////
Private Function m_TableExist(ByVal TableName As String) As Boolean
Dim i As Integer
m_TableExist = False
If m_CT.Tables.Count > 0 Then
For i = 0 To m_CT.Tables.Count - 1
If LCase(m_CT.Tables(i).Name) = LCase(TableName) Then
m_TableExist = True
Exit Function
End If
Next i
End If
End Function
Private Function m_FieldType(ByVal FieldType As String) As DataTypeEnum
Select Case LCase(FieldType)
Case "string"
m_FieldType = adVarWChar
Case "text"
m_FieldType = adLongVarWChar
Case "date"
m_FieldType = adDate
Case "currency"
m_FieldType = adCurrency
Case "boolean"
m_FieldType = adBoolean
Case "double"
m_FieldType = adDouble
Case "integer"
m_FieldType = adInteger
Case "guid"
m_FieldType = adGUID
Case "single"
m_FieldType = adSingle
Case "longbinary"
m_FieldType = adLongVarBinary
Case "byte"
m_FieldType = adUnsignedTinyInt
Case "short"
m_FieldType = adSmallInt
Case Else
m_FieldType = adVarWChar
End Select
End Function
'//////////////////////////////////////////////////////////////////////////////
'//
'// 公有方法
'//
'//////////////////////////////////////////////////////////////////////////////
Public Sub SetDbFile(ByVal FilePath As String)
m_ConnStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & ";"
If Dir(FilePath) = "" Then
m_CT.Create m_ConnStr
End If
m_CT.ActiveConnection = m_ConnStr
End Sub
Public Sub CreateTable(ByVal TableName As String, ByVal FieldNames As String)
Dim tb As New ADOX.Table
Dim fields() As String
Dim field As String
Dim i As Integer
Dim col As ADOX.Column
If Len(m_ConnStr) <= 0 Then
Err.Raise 105, , "[SunSoft]未指定数据库连接,请检查"
End If
If Not m_TableExist(TableName) Then
tb.Name = TableName
fields = Split(FieldNames, ",")
For i = 0 To UBound(fields)
Set col = New ADOX.Column
col.Name = Split(fields(i), ":")(0)
col.Type = m_FieldType(Split(fields(i), ":")(1))
If Split(fields(i), ":")(1) = "boolean" Then
col.Attributes = ADOX.ColumnAttributesEnum.adColFixed
Else
col.Attributes = ADOX.ColumnAttributesEnum.adColNullable
End If
'tb.Columns.Append Split(fields(i), ":")(0), m_FieldType(Split(fields(i), ":")(1))
tb.Columns.Append col
Next i
'tb.Columns.Append "adBinary", adBinary
'tb.Columns.Append "adBoolean", adBoolean
'tb.Columns.Append "adCurrency", adCurrency
'tb.Columns.Append "adDate", adDate
'tb.Columns.Append "adDouble", adDouble
'tb.Columns.Append "adGUID", adGUID
'tb.Columns.Append "adInteger", adInteger
'tb.Columns.Append "adLongVarBinary", adLongVarBinary
'tb.Columns.Append "adLongVarWChar", adLongVarWChar
'tb.Columns.Append "adSingle", adSingle
'tb.Columns.Append "adSmallInt", adSmallInt
'tb.Columns.Append "adUnsignedTinyInt", adUnsignedTinyInt
'tb.Columns.Append "adVarBinary", adVarBinary
'tb.Columns.Append "adVarWChar", adVarWChar
'tb.Columns.Append "adWChar", adWChar
m_CT.Tables.Append tb
End If
End Sub
Public Sub InitDbFromModels(ParamArray dbModels())
Dim model As DBModel
Dim i As Integer
For i = LBound(dbModels) To UBound(dbModels)
Set model = dbModels(i)
CreateTable model.TableName, model.TableFields
Next
End Sub