001.
Imports
System
002.
Imports
System.Data
003.
Imports
System.Data.OleDb
004.
Imports
System.IO
005.
Imports
System.Text
006.
Imports
System.Collections
007.
Imports
System.Collections.Generic
008.
009.
Imports
System.Runtime.CompilerServices
010.
011.
012.
013.
014.
015.
016.
017.
018.
Namespace
OleDbWithMSAccess
019.
020.
Public
Enum
MSAccessVersionInfo
021.
AccessCommon = 1
022.
Access2007 = 2
023.
024.
End
Enum
025.
026.
Public
Class
MSAccessConnectionArgs
027.
028.
Private
Const
constAccess2007ConnectionStringFrame =
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};"
_
029.
&
"Persist Security Info=False;"
030.
Private
Const
constAccess2007ConnectionStringFrameWithUserNameAndPassword =
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};"
_
031.
&
"Jet OLEDB:Database Password={1};"
032.
033.
Private
Const
constAccessCommonConnectionStringFrame =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};"
_
034.
&
"Persist Security Info=False;"
035.
Private
Const
constAccessCommonConnectionStringFrameWithUserNameAndPassword =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};"
_
036.
&
"User Id=admin;Password={1};"
037.
038.
Private
Const
constMsAccessCommandParameterPrefix
As
String
=
"@"
039.
040.
Private
_VersionInfo
As
MSAccessVersionInfo = MSAccessVersionInfo.AccessCommon
041.
Private
_DataBaseFile
As
String
=
String
.Empty
042.
Private
_HasUserName
As
Boolean
=
False
043.
044.
Private
_Password
As
String
=
String
.Empty
045.
046.
Public
Property
VersionInfo()
As
MSAccessVersionInfo
047.
Get
048.
Return
_VersionInfo
049.
End
Get
050.
Set
(
ByVal
value
As
MSAccessVersionInfo)
051.
_VersionInfo = value
052.
End
Set
053.
End
Property
054.
Public
Property
DataBaseFileName()
As
String
055.
Get
056.
Return
_DataBaseFile
057.
End
Get
058.
Set
(
ByVal
value
As
String
)
059.
_DataBaseFile = value
060.
If
((_UserName.Length = 0)
And
(_Password.Length = 0))
Then
061.
_HasUserName =
False
062.
Else
063.
_HasUserName =
True
064.
End
If
065.
066.
067.
Dim
fileExtension
As
String
= _DataBaseFile.Substring(_DataBaseFile.LastIndexOf(
"."
c) + 1).ToUpper()
068.
If
(fileExtension.Equals(
"ACCDB"
))
Then
069.
_VersionInfo = MSAccessVersionInfo.Access2007
070.
Else
071.
_VersionInfo = MSAccessVersionInfo.AccessCommon
072.
End
If
073.
074.
End
Set
075.
End
Property
076.
077.
078.
079.
080.
081.
082.
083.
084.
085.
086.
087.
088.
089.
090.
Public
Property
Password()
As
String
091.
Get
092.
Return
_Password
093.
End
Get
094.
Set
(
ByVal
value
As
String
)
095.
_Password = value
096.
If
(_Password.Length = 0)
Then
097.
_HasUserName =
False
098.
Else
099.
_HasUserName =
True
100.
End
If
101.
End
Set
102.
End
Property
103.
Public
Property
HasUserNameAndPassword()
As
Boolean
104.
Get
105.
Return
_HasUserName
106.
End
Get
107.
Set
(
ByVal
value
As
Boolean
)
108.
_HasUserName = value
109.
End
Set
110.
End
Property
111.
112.
Public
Sub
New
()
113.
End
Sub
114.
115.
Public
Sub
New
(
ByVal
argVersionInfo
As
MSAccessVersionInfo)
116.
Me
.VersionInfo = argVersionInfo
117.
End
Sub
118.
Public
Sub
New
(
ByVal
argVersionInfo
As
MSAccessVersionInfo,
ByVal
argDatabaseFileName
As
String
)
119.
Me
.VersionInfo = argVersionInfo
120.
Me
.DataBaseFileName = argDatabaseFileName
121.
End
Sub
122.
123.
Public
Function
IsReadyToConnect()
As
Boolean
124.
Return
(
Me
.DataBaseFileName.Length = 0)
125.
End
Function
126.
127.
Public
Function
GetConnectionString()
As
String
128.
Dim
retStr
As
String
=
String
.Empty
129.
Dim
ConnStr
As
String
=
String
.Empty
130.
131.
If
(
Me
.VersionInfo = MSAccessVersionInfo.AccessCommon)
Then
132.
133.
If
(
Me
.HasUserNameAndPassword)
Then
134.
retStr =
String
.Format(constAccessCommonConnectionStringFrameWithUserNameAndPassword _
135.
,
Me
.DataBaseFileName,
Me
.Password)
136.
Else
137.
retStr =
String
.Format(constAccessCommonConnectionStringFrame _
138.
,
Me
.DataBaseFileName)
139.
End
If
140.
Else
141.
142.
If
(
Me
.HasUserNameAndPassword)
Then
143.
retStr =
String
.Format(constAccess2007ConnectionStringFrameWithUserNameAndPassword _
144.
,
Me
.DataBaseFileName,
Me
.Password)
145.
Else
146.
retStr =
String
.Format(constAccess2007ConnectionStringFrame _
147.
,
Me
.DataBaseFileName)
148.
End
If
149.
150.
End
If
151.
152.
Return
retStr
153.
End
Function
154.
155.
End
Class
156.
157.
Public
Class
MsAccessSQLCommandArgs
158.
159.
Public
ConnectionArgs
As
MSAccessConnectionArgs =
New
MSAccessConnectionArgs()
160.
161.
162.
163.
164.
165.
166.
Public
CommandStr
As
String
=
String
.Empty
167.
Public
CommandParameters
As
Hashtable =
New
Hashtable()
168.
169.
Public
Sub
New
()
170.
171.
End
Sub
172.
173.
Public
Sub
ClearAll()
174.
CommandStr =
String
.Empty
175.
CommandParameters =
New
Hashtable()
176.
End
Sub
177.
178.
Public
Sub
ClearParameters()
179.
CommandParameters =
New
Hashtable()
180.
End
Sub
181.
182.
Public
Sub
AddParameter(
ByVal
argKey
As
String
,
ByVal
argValue
As
Object
)
183.
CommandParameters.Add(argKey, argValue)
184.
End
Sub
185.
186.
End
Class
187.
188.
Module
MsAccessExtension
189.
190.
Private
Const
constMsAccessCommandParameterPrefix
As
String
=
"@"
191.
192.
<Extension()> _
193.
Public
Sub
ExecuteCommand(
ByVal
e
As
MsAccessSQLCommandArgs)
194.
Using myConnection
As
OleDbConnection _
195.
=
New
OleDbConnection(e.ConnectionArgs.GetConnectionString())
196.
197.
Dim
myCommand
As
OleDbCommand _
198.
=
New
OleDbCommand(e.CommandStr, myConnection)
199.
200.
myCommand.CommandType = CommandType.Text
201.
202.
If
(e.CommandParameters.Count > 0)
Then
203.
204.
For
Each
keyStr
As
String
In
e.CommandParameters.Keys
205.
Dim
tempKey
As
String
= keyStr
206.
If
(
Not
keyStr.Substring(0, 1).Equals(constMsAccessCommandParameterPrefix))
Then
207.
tempKey = constMsAccessCommandParameterPrefix & tempKey
208.
End
If
209.
myCommand.Parameters.AddWithValue(tempKey, e.CommandParameters(keyStr))
210.
Next
211.
212.
End
If
213.
214.
Try
215.
myConnection.Open()
216.
myCommand.ExecuteNonQuery()
217.
Catch
ex
As
Exception
218.
Throw
New
Exception(ex.Message)
219.
Finally
220.
If
Not
(myConnection
Is
Nothing
)
Then
221.
myConnection.Close()
222.
End
If
223.
End
Try
224.
225.
End
Using
226.
End
Sub
227.
228.
<Extension()> _
229.
Public
Function
ExecuteCommandScalar(
ByVal
e
As
MsAccessSQLCommandArgs)
As
Object
230.
231.
Dim
oRet
As
Object
=
Nothing
232.
233.
Using myConnection
As
OleDbConnection _
234.
=
New
OleDbConnection(e.ConnectionArgs.GetConnectionString())
235.
236.
Dim
myCommand
As
OleDbCommand _
237.
=
New
OleDbCommand(e.CommandStr, myConnection)
238.
239.
myCommand.CommandType = CommandType.Text
240.
241.
If
(e.CommandParameters.Count > 0)
Then
242.
243.
For
Each
keyStr
As
String
In
e.CommandParameters.Keys
244.
Dim
tempKey
As
String
= keyStr
245.
If
(
Not
keyStr.Substring(0, 1).Equals(constMsAccessCommandParameterPrefix))
Then
246.
tempKey = constMsAccessCommandParameterPrefix & tempKey
247.
End
If
248.
myCommand.Parameters.AddWithValue(tempKey, e.CommandParameters(keyStr))
249.
Next
250.
251.
End
If
252.
253.
Try
254.
myConnection.Open()
255.
oRet = myCommand.ExecuteScalar()
256.
Catch
ex
As
Exception
257.
Throw
New
Exception(ex.Message)
258.
Finally
259.
If
Not
(myConnection
Is
Nothing
)
Then
260.
myConnection.Close()
261.
End
If
262.
End
Try
263.
264.
Return
oRet
265.
266.
End
Using
267.
End
Function
268.
269.
270.
271.
272.
273.
274.
275.
<Extension()> _
276.
Public
Function
GetDataTable(
ByVal
e
As
MsAccessSQLCommandArgs)
As
DataTable
277.
278.
Dim
dtRet
As
DataTable =
New
DataTable()
279.
280.
Using myConnection
As
OleDbConnection _
281.
=
New
OleDbConnection(e.ConnectionArgs.GetConnectionString())
282.
283.
Dim
myCommand
As
OleDbCommand _
284.
=
New
OleDbCommand(e.CommandStr, myConnection)
285.
286.
myCommand.CommandType = CommandType.Text
287.
288.
If
(e.CommandParameters.Count > 0)
Then
289.
290.
For
Each
keyStr
As
String
In
e.CommandParameters.Keys
291.
Dim
tempKey
As
String
= keyStr
292.
If
(
Not
keyStr.Substring(0, 1).Equals(constMsAccessCommandParameterPrefix))
Then
293.
tempKey = constMsAccessCommandParameterPrefix & tempKey
294.
End
If
295.
myCommand.Parameters.AddWithValue(tempKey, e.CommandParameters(keyStr))
296.
Next
297.
298.
End
If
299.
Dim
myDataAdaptor
As
OleDbDataAdapter =
New
OleDbDataAdapter(myCommand)
300.
301.
Try
302.
myConnection.Open()
303.
myDataAdaptor.Fill(dtRet)
304.
305.
Catch
ex
As
Exception
306.
Throw
New
Exception(ex.Message)
307.
Finally
308.
If
Not
(myConnection
Is
Nothing
)
Then
309.
myConnection.Close()
310.
End
If
311.
End
Try
312.
313.
Return
dtRet
314.
315.
End
Using
316.
End
Function
317.
318.
End
Module
319.
320.
End
Namespace