-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataObjectBase.vb
320 lines (308 loc) · 12.2 KB
/
DataObjectBase.vb
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
Imports System.Data.SqlClient
Public MustInherit Class DataObjectBase
Implements IDisposable
Public Enum TransactionType
NoTransaction = 0
RequireTransaction = 1
End Enum
Private InternalSqlStr As String = "Select * from "
Protected oSqlConnection As SqlConnection
Protected oTransactionType As TransactionType = TransactionType.NoTransaction
Protected oCurrentTransaction As SqlTransaction
Protected bIsLocalConenction As Boolean = False
Public Sub SetParent(ByVal dat As DataObjectBase)
oSqlConnection = dat.oSqlConnection
oCurrentTransaction = dat.oCurrentTransaction
End Sub
Public Sub Commit()
If Not oCurrentTransaction Is Nothing Then
oCurrentTransaction.Commit()
oCurrentTransaction.Dispose()
oCurrentTransaction = Nothing
oSqlConnection.Close()
oSqlConnection.Dispose()
Else
Throw New Exception("No Transaction to commit")
End If
End Sub
Public Sub RollBack()
If Not oCurrentTransaction Is Nothing Then
oCurrentTransaction.Rollback()
oCurrentTransaction.Dispose()
oCurrentTransaction = Nothing
oSqlConnection.Close()
oSqlConnection.Dispose()
Else
Throw New Exception("No Transaction to RollBack")
End If
End Sub
Public MustOverride ReadOnly Property TableName() As String
Public Property TransactionMode() As TransactionType
Get
Return oTransactionType
End Get
Set(ByVal Value As TransactionType)
oTransactionType = Value
End Set
End Property
Public Sub RunSqlSt(ByVal SqlStr As String, ByRef DataReader As SqlDataReader)
Dim cmd As SqlCommand
Try
cmd = CreateCommand(SqlStr)
DataReader = cmd.ExecuteReader(CommandBehavior.Default)
Catch ex As Exception
DataReader = Nothing
If Not cmd Is Nothing Then cmd.Dispose()
Throw New Exception("Data Command " + SqlStr + " failed ->" + ex.Message)
End Try
cmd.Dispose()
cmd = Nothing
End Sub
Public Sub RunSqlSt(ByVal SqlStr As String, ByRef Dataset As DataSet)
Dim cmd As SqlCommand
Dim Sqladp As SqlDataAdapter
Try
cmd = CreateCommand(SqlStr)
Sqladp = New SqlDataAdapter(cmd)
Sqladp.Fill(Dataset)
Catch ex As Exception
Throw New Exception("Data Command " + SqlStr + " failed ->" + ex.Message)
End Try
cmd.Dispose()
cmd = Nothing
Sqladp.Dispose()
Sqladp = Nothing
End Sub
Protected Function RunProcedure(ByVal ProcName As String) As Int64
Dim result As Int64
Dim cmd As SqlCommand
Try
cmd = CreateCommand(ProcName, Nothing)
result = CType(cmd.ExecuteScalar(), Int64)
Catch ex As Exception
If Not cmd Is Nothing Then cmd.Dispose()
Throw New Exception("Data Command " + ProcName + " failed ->" + ex.Message)
End Try
cmd.Dispose()
cmd = Nothing
Return result
End Function
Protected Function RunProcedure(ByVal ProcName As String, ByVal Params As SqlParameter()) As Object
Dim result As Object
Dim cmd As SqlCommand
Try
cmd = CreateCommand(ProcName, Params)
result = cmd.ExecuteScalar
Catch ex As Exception
If Not cmd Is Nothing Then cmd.Dispose()
Throw New Exception("Data Command " + ProcName + " failed ->" + ex.Message)
End Try
cmd.Dispose()
cmd = Nothing
Return result
End Function
Protected Sub RunProcedure(ByVal ProcName As String, ByRef Datareader As SqlDataReader)
Dim cmd As SqlCommand
Try
cmd = CreateCommand(ProcName, Nothing)
Datareader = cmd.ExecuteReader(CommandBehavior.Default)
Catch ex As Exception
Datareader = Nothing
If Not cmd Is Nothing Then cmd.Dispose()
Throw New Exception("Data Command " + ProcName + " failed ->" + ex.Message)
End Try
cmd.Dispose()
cmd = Nothing
End Sub
Protected Sub RunProcedure(ByVal ProcName As String, ByVal Params As SqlParameter(), ByRef Datareader As SqlDataReader)
Dim cmd As SqlCommand
Try
cmd = CreateCommand(ProcName, Params)
Datareader = cmd.ExecuteReader(CommandBehavior.Default)
Catch ex As Exception
Datareader = Nothing
If Not cmd Is Nothing Then cmd.Dispose()
cmd = Nothing
Throw New Exception("Data Command " + ProcName + " failed ->" + ex.Message)
End Try
cmd.Dispose()
cmd = Nothing
End Sub
Protected Sub RunProcedure(ByVal ProcName As String, ByVal Params As SqlParameter(), ByRef DataSet As DataSet)
Dim cmd As SqlCommand
Dim SqlAdb As SqlDataAdapter
Try
cmd = CreateCommand(ProcName, Params)
SqlAdb = New SqlDataAdapter(cmd)
SqlAdb.Fill(DataSet)
Catch ex As Exception
If Not cmd Is Nothing Then cmd.Dispose()
If Not sqladb Is Nothing Then SqlAdb.Dispose()
cmd = Nothing
SqlAdb = Nothing
Throw New Exception("Data Command " + ProcName + " failed ->" + ex.Message)
End Try
End Sub
Protected Sub RunProcedure(ByVal ProcName As String, ByRef DataSet As DataSet)
Dim cmd As SqlCommand
Dim SqlAdb As SqlDataAdapter
Try
cmd = CreateCommand(ProcName, Nothing)
SqlAdb = New SqlDataAdapter(cmd)
SqlAdb.Fill(DataSet)
Catch ex As Exception
If Not cmd Is Nothing Then cmd.Dispose()
If Not sqladb Is Nothing Then SqlAdb.Dispose()
cmd = Nothing
SqlAdb = Nothing
Throw New Exception("Data Command " + ProcName + " failed ->" + ex.Message)
End Try
End Sub
Protected Function MakeParameter(ByVal ParamName As String, ByVal value As Object) As SqlParameter
Dim Param As New SqlParameter(ParamName, value)
Param.Direction = ParameterDirection.Input
Return Param
End Function
Protected Function MakeParameter(ByVal ParamName As String, ByVal value As Object, ByVal Direction As ParameterDirection) As SqlParameter
Dim Param As New SqlParameter(ParamName, value)
Param.Direction = Direction
Return Param
End Function
Protected Function CreateCommand(ByVal ProcName As String, ByVal Params As SqlParameter()) As SqlCommand
Dim cmd As SqlCommand
Dim param As SqlParameter
Open()
cmd = New SqlCommand(ProcName, oSqlConnection)
cmd.CommandType = CommandType.StoredProcedure
If Not Params Is Nothing Then
For Each param In Params
cmd.Parameters.Add(param)
Next
End If
If Not oCurrentTransaction Is Nothing Then
cmd.Transaction = oCurrentTransaction
End If
Return cmd
End Function
Protected Function CreateCommand(ByVal SqlString As String) As SqlCommand
Dim cmd As SqlCommand
Open()
cmd = New SqlCommand(SqlString, oSqlConnection)
If Not oCurrentTransaction Is Nothing Then
cmd.Transaction = oCurrentTransaction
End If
Return cmd
End Function
Protected Sub Open()
If oSqlConnection Is Nothing Then
oSqlConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("DataString"))
oSqlConnection.Open()
If oTransactionType = TransactionType.RequireTransaction Then oCurrentTransaction = oSqlConnection.BeginTransaction
bIsLocalConenction = True
Else
If oSqlConnection.State = ConnectionState.Closed Or oSqlConnection.State = ConnectionState.Broken Then
Throw New Exception("DataBase Conenction is Broken")
End If
bIsLocalConenction = False
End If
End Sub
Protected Sub Close()
If bIsLocalConenction Then
If Not oSqlConnection Is Nothing AndAlso (Not oSqlConnection.State = ConnectionState.Broken OrElse oSqlConnection.State = ConnectionState.Closed) Then
oSqlConnection.Close()
oSqlConnection.Dispose()
oSqlConnection = Nothing
End If
End If
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
Close()
End Sub
Public Function ListAll() As DataSet
Dim sqlDs As New DataSet
RunSqlSt(InternalSqlStr + TableName, sqlDs)
Return sqlDs
End Function
Public Function ListFiltered(ByVal FilterStr As String) As DataSet
Dim sqlDs As New DataSet
RunSqlSt(InternalSqlStr + TableName + " Where " + FilterStr, sqlDs)
Return sqlDs
End Function
Public Function ListDataReaderAll() As SqlDataReader
Dim sqlDs As SqlDataReader
RunSqlSt(InternalSqlStr + TableName, sqlDs)
Return sqlDs
End Function
Public Function ListDataReaderFiltered(ByVal FilterStr As String) As SqlDataReader
Dim sqlDs As SqlDataReader
RunSqlSt(InternalSqlStr + TableName + " Where " + FilterStr, sqlDs)
Return sqlDs
End Function
Public Class DatabaseUtility
'''<summary>
'''Validate if the current passed object is a valid date ( lager than 1753)
'''</summary>
Public Shared Function IsvalidDate(ByVal obj As Object) As Boolean
Dim tmpdate As Date = CDate(obj)
If tmpdate.Year > 1753 Then Return True
Return False
End Function
'''<summary>
'''Validate if the current passed object is a valid Number
'''</summary>
Public Shared Function IsNumber(ByVal obj As Object) As Boolean
If obj.ToString.IndexOf("-") >= 0 Then Return False
Return True
End Function
'''<summary>
'''Validate if the current passed object is a null database value , returned value is new date object
'''</summary>
Public Shared Function DBNullToDate(ByVal obj As Object) As Date
If IsDBNull(obj) Then Return New Date
Return CDate(obj)
End Function
'''<summary>
'''Validate if the current passed object is a null database value , returend value is an empty string
'''</summary>
Public Shared Function DBNullToString(ByVal obj As Object) As String
If IsDBNull(obj) Then Return String.Empty
Return CStr(obj)
End Function
'''<summary>
'''Check if the string is empty the return value would be a database null value
'''</summary>
Public Shared Function StringToDBNull(ByVal obj As String) As Object
If obj = String.Empty Then Return DBNull.Value
Return obj
End Function
'''<summary>
'''Check if the date is empty the return value would be a database null value
'''</summary>
Public Shared Function DateToDBNull(ByVal obj As Date) As Object
If obj.Year < 1753 Then Return DBNull.Value
Return obj
End Function
'''<summary>
'''Check if the Number is 0 the return value would be a database null value
'''</summary>
Public Shared Function IntegerToDBNull(ByVal obj As Int64) As Object
If obj = 0 Then Return DBNull.Value
Return obj
End Function
'''<summary>
'''Check if the passed object is database null value then return 0
'''</summary>
Public Shared Function DBNullToInteger(ByVal obj As Object) As Int64
If IsDBNull(obj) Then Return 0
Return CInt(obj)
End Function
'''<summary>
'''Check if the the passed date is a valid date and return a string otherwise return an empty string
'''</summary>
Public Shared Function IsDate(ByVal obj As Object) As String
Dim tmpdate As Date = CDate(obj)
If tmpdate.Year > 1753 Then Return tmpdate.ToShortDateString
Return ""
End Function
End Class
End Class