-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsDatabase.vb
1226 lines (1130 loc) · 53.2 KB
/
clsDatabase.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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports System.Data.OleDb
Imports System.IO
Imports System.Text
Imports MySql.Data.MySqlClient
Imports log4net
Imports log4net.Config
Imports System.Configuration
Imports System.Reflection
Namespace Onling
''' <summary>
''' Onling.com
''' Use and modify at will...
''' This was intended to replace all the silly functions that I had...
''' </summary>
''' <remarks></remarks>
#Region "Database Connection Class"
Public Class clsDatabase
''' <summary>
''' A Database connection for .NET
''' </summary>
''' <remarks>
''' This class does not hold open a connection but
''' instead is stateless: for each request it
''' connects, performs the request and disconnects.
''' </remarks>
Private Shared ReadOnly _log As ILog = LogManager.GetLogger(GetType(clsDatabase))
#Region "LOCAL DECLARATIONS"
Private _Sqlhostname As String
Private _Sqlusername As String
Private _Sqlpassword As String
Private _SqlDBName As String
Private _MySqlhostname As String
Private _MySqlusername As String
Private _MySqlpassword As String
Private _MySqlDBName As String
Private _MySqlport As String
Private _Odbchostname As String
Private _Odbcusername As String
Private _Odbcpassword As String
Private _OdbcDBName As String
Private _Odbcport As String
Private _OdbcDSN As String
Public MsSqlDBConn As New SqlConnection
Public MySqlDBConn As New MySqlConnection
Public ODBCDBConn As New OdbcConnection
#End Region
#Region "CONSTRUCTORS"
''' <summary>
''' Manual Constructor
''' </summary>
''' <remarks>All has to be passed manually</remarks>
Sub New()
_log.Info("Starting " & MethodBase.GetCurrentMethod().ToString())
End Sub
#End Region
#Region "Sql Properties"
''' <summary>
''' Sql Hostname
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property SqlHostname() As String
Get
Return _Sqlhostname
End Get
Set(ByVal value As String)
_Sqlhostname = value
End Set
End Property
''' <summary>
''' Sql Username
''' </summary>
''' <value></value>
''' <remarks></remarks>
Public Property SqlUsername() As String
Get
Return _Sqlusername
End Get
Set(ByVal value As String)
_Sqlusername = value
End Set
End Property
''' <summary>
''' Sql Password
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property SqlPassword() As String
Get
Return _Sqlpassword
End Get
Set(ByVal value As String)
_Sqlpassword = value
End Set
End Property
''' <summary>
''' Sql DBName
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property SqlDBName() As String
Get
Return _SqlDBName
End Get
Set(ByVal value As String)
_SqlDBName = value
End Set
End Property
#End Region
#Region "MsSQL Connection"
''' <summary>
''' Makes a Connection to MsSql
''' </summary>
''' <param name="Trusted">True or False if you have a trusted connection</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function SqlConn(Optional ByVal Trusted As Boolean = True) As SqlConnection
Try
'Dim MsSqlDBConn As New SqlConnection
If Trusted Then
MsSqlDBConn.ConnectionString = "data source=" & _Sqlhostname & ";" & _
"Initial catalog=""" & _SqlDBName & """;" & _
"Integrated Security=True;" & _
"MultipleActiveResultSets=True;"
'Network Library=DBMSSOCN;"
_log.Info(Environment.UserName & " Opening Database Trusted IP: " & _Sqlhostname & " DBName " & _SqlDBName)
Else
MsSqlDBConn.ConnectionString = "data source=" & _Sqlhostname & ";" & _
"Initial catalog=" & _SqlDBName & ";" & _
"User ID=" & _Sqlusername & ";" & _
"Password=" & _Sqlpassword & ";" & _
"MultipleActiveResultSets=True;"
_log.Info(Environment.UserName & " Opening Database IP: " & _Sqlhostname & " DBName " & _SqlDBName)
_log.Info(Environment.UserName & " Opened Database Username : " & _Sqlusername & " Password " & _Sqlpassword)
End If
MsSqlDBConn.Open()
Return MsSqlDBConn
Catch ex As SqlException
Throw New ApplicationException("Connection to " & _Sqlhostname & " was not Successfull.")
_log.Error("Connection to " & _Sqlhostname & " was not Successfull.")
End Try
End Function
#End Region
#Region "MySql Properties"
''' <summary>
''' MySql Hostname
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property MySqlHostname() As String
Get
Return _MySqlhostname
End Get
Set(ByVal value As String)
_MySqlhostname = value
End Set
End Property
''' <summary>
''' MySql Username
''' </summary>
''' <value></value>
''' <remarks></remarks>
Public Property MySqlUsername() As String
Get
Return _MySqlusername
End Get
Set(ByVal value As String)
_MySqlusername = value
End Set
End Property
''' <summary>
''' MySql Password
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property MySqlPassword() As String
Get
Return _MySqlpassword
End Get
Set(ByVal value As String)
_MySqlpassword = value
End Set
End Property
''' <summary>
''' MySql DBName
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property MySqlDBName() As String
Get
Return _MySqlDBName
End Get
Set(ByVal value As String)
_MySqlDBName = value
End Set
End Property
''' <summary>
''' MySql Port
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property MySqlPort() As String
Get
Return _MySqlport
End Get
Set(ByVal value As String)
_MySqlport = value
End Set
End Property
#End Region
#Region "MySQL Connection"
Public Function MySqlConn(Optional ByVal ConnectionString As String = "", Optional ByVal MySqlport As Integer = 3306)
Try
If ConnectionString.Length > 0 Then
MySqlDBConn.ConnectionString = ConnectionString
Else
'Dim OdbcDBConn As New OdbcConnection
MySqlDBConn.ConnectionString = "server=" & _MySqlhostname & ";" &
"database=" & _MySqlDBName & ";" &
"uid=" & _MySqlusername & ";" &
"pwd=" & _MySqlpassword & ";" &
"port=" & _MySqlport & ";"
End If
'oOdbcConn.ConnectionString = _
'"User ID=uid;" & _
'"Password=pw;" & _
'"Host=ip;" & _
'"Port=3306;" & _
'"Database=db;" & _
'"Direct=true;" & _
'"Protocol=TCP;" & _
'"Compress=false;" & _
'"Pooling=true;" & _
'"Min Pool Size=0;" & _
'"Max Pool Size=100;" & _
'"Connection Lifetime=0"
'OdbcConn.ConnectionString = "Host=" & DBServer & ";" & _
' "Database=" & DBName & ";" & _
' "User ID=" & DBLogin & ";" & _
' "Password=" & DBPassword & ";" & _
' "Port=" & DBPort & ";" & _
' "Direct=True;" & _
' "Protocol=TCP;" & _
' "Compress=false;" & _
' "Min Pool Size=0;" & _
' "Max Pool Size=100;" & _
' "Connection Lifetime=0"
_log.Info(Environment.UserName & " Opened MySql Database : " & _MySqlhostname & " DBName " & _MySqlDBName)
MySqlDBConn.Open()
Return True
Catch ex As MySqlException
If ex.Number = 0 Then
Throw New ApplicationException("Connection to " & _MySqlhostname & " was not Successfull.")
_log.Error("Connection to " & _MySqlhostname & " was not Successfull.")
ElseIf ex.Number = 1045 Then
Throw New ApplicationException("Invalid username/password, please try again")
_log.Error("Connection to " & _MySqlhostname & " was not Successfull as Invalid username/password, please try again")
End If
Throw New ApplicationException("Connection to " & _MySqlhostname & " was not Successfull.")
_log.Error("Connection to " & _MySqlhostname & " was not Successfull with error number: " & ex.Number.ToString)
End Try
End Function
#End Region
#Region "ODBC Properties"
''' <summary>
''' ODBC Hostname
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ODBCHostname() As String
Get
Return _Odbchostname
End Get
Set(ByVal value As String)
_Odbchostname = value
End Set
End Property
''' <summary>
''' ODBC Username
''' </summary>
''' <value></value>
''' <remarks></remarks>
Public Property ODBCUsername() As String
Get
Return _Odbcusername
End Get
Set(ByVal value As String)
_Odbcusername = value
End Set
End Property
''' <summary>
''' ODBC Password
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ODBCPassword() As String
Get
Return _Odbcpassword
End Get
Set(ByVal value As String)
_Odbcpassword = value
End Set
End Property
''' <summary>
''' ODBC DBName
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ODBCDBName() As String
Get
Return _OdbcDBName
End Get
Set(ByVal value As String)
_OdbcDBName = value
End Set
End Property
''' <summary>
''' ODBC Port
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ODBCPort() As String
Get
Return _Odbcport
End Get
Set(ByVal value As String)
_Odbcport = value
End Set
End Property
''' <summary>
''' ODBC DSN
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks>DSN Name</remarks>
Public Property ODBCDSN() As String
Get
Return _OdbcDSN
End Get
Set(ByVal value As String)
_OdbcDSN = value
End Set
End Property
#End Region
#Region "ODBC Connection"
''' <summary>
''' ODBC Connection
''' </summary>
''' <param name="DBType">Database Type MySQL, TranSoft or DSN</param>
''' <returns></returns>
''' <remarks>Opens a connection via ODBC</remarks>
Public Function ODBCConn(ByVal DBType As String) As OdbcConnection
Try
'UpperCase for user input
DBType = DBType.ToUpper()
If DBType = "MYSQL" Then
ODBCDBConn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" & _
"Server=" & _Odbchostname & ";" & _
"Port=" & _Odbcport & ";" & _
"Option=3;" & _
"Stmt=;" & _
"Database=" & _OdbcDBName & ";" & _
"Uid=" & _Odbcusername & ";" & _
"Pwd=" & _Odbcpassword & ""
ODBCDBConn.Open()
End If
If DBType = "TRANSOFT" Then
ODBCDBConn.ConnectionString = "DRIVER={Transoft ODBC Driver};" & _
"TSDSN=" & _OdbcDSN & ";" & _
"Server=" & _Odbchostname & ";" & _
"Port=" & _Odbcport & ";" & _
"Timeout=200;" & _
"Description="""
ODBCDBConn.Open()
End If
If DBType = "DSN" Then
ODBCDBConn.ConnectionString = "DSN=" & _OdbcDSN & ";Uid=" & _Odbcusername & ";" & _
"Pwd=" & _Odbcpassword & ""
ODBCDBConn.Open()
End If
Return ODBCDBConn
Catch ex As Exception
Throw New ArgumentException()
End Try
'Dim csLogon As String
'cLogon = "DRIVER={MySQL ODBC 3.51 Driver};" & "Server=" & DBServer & ";Port=3306;Database=xxxxxx;Uid=xxxxxx;Pwd=xxxxxx;Option=" & (1 + 2 + 8 + 32 + 2048 + 16384)
'adoConn = New System.Data.Odbc.OdbcConnection(csLogon)
End Function
#End Region
#Region "Public Functions"
''' <summary>
''' Gets the MsSql Connection State Opened or Closed
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function MsSqlState() As Boolean
If MsSqlDBConn.State = ConnectionState.Closed Then
Return False
Else
Return True
End If
End Function
''' <summary>
''' Gets the MySql Connection State Opened or Closed
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function MySqlState() As Boolean
If MySqlDBConn.State = ConnectionState.Closed Then
Return False
Else
Return True
End If
End Function
''' <summary>
''' Gets the Odbc Connection State Opened or Closed
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function OdbcState() As Boolean
If ODBCDBConn.State = ConnectionState.Closed Then
Return False
Else
Return True
End If
End Function
#End Region
End Class
#End Region
#Region " SQLCommands "
Public Class clsSQLExecutes
Inherits Onling.clsDatabase
''' <summary>
''' A Sql Data Provider Class
''' </summary>
''' <remarks>This class is used to ease the commands to update the database</remarks>
Private Shared ReadOnly _log As ILog = LogManager.GetLogger(GetType(clsSQLExecutes))
#Region "LOCAL DECLARATIONS"
#End Region
#Region "CONSRUCTORS"
''' <summary>
''' Blank constructor
''' </summary>
''' <remarks></remarks>
Sub New()
_log.Info("Starting " & MethodBase.GetCurrentMethod().ToString())
End Sub
#End Region
#Region "PROPERTIES"
#End Region
#Region "PRIVATE METHODS"
Private Sub AssignParameters(ByVal cmd As SqlCommand, ByVal cmdParameters() As SqlParameter)
If (cmdParameters Is Nothing) Then Exit Sub
For Each p As SqlParameter In cmdParameters
cmd.Parameters.Add(p)
_log.Info("Adding Command Parameter " & p.Value.ToString)
Next
End Sub
Private Sub AssignParameters(ByVal cmd As OdbcCommand, ByVal cmdParameters() As OdbcParameter)
If (cmdParameters Is Nothing) Then Exit Sub
For Each p As OdbcParameter In cmdParameters
cmd.Parameters.Add(p)
_log.Info("Adding Command Parameter " & p.Value.ToString)
Next
End Sub
Private Sub AssignParameters(ByVal cmd As SqlCommand, ByVal parameterValues() As Object)
If Not (cmd.Parameters.Count - 1 = parameterValues.Length) Then Throw New ApplicationException("Stored procedure's parameters and parameter values does not match.")
Dim i As Integer
For Each param As SqlParameter In cmd.Parameters
If Not (param.Direction = ParameterDirection.Output) AndAlso Not (param.Direction = ParameterDirection.ReturnValue) Then
param.Value = parameterValues(i)
_log.Info("Adding Parameter " & param.Value.ToString)
i += 1
End If
Next
End Sub
Private Sub AssignParameters(ByVal cmd As OdbcCommand, ByVal parameterValues() As Object)
If Not (cmd.Parameters.Count - 1 = parameterValues.Length) Then Throw New ApplicationException("Stored procedure's parameters and parameter values does not match.")
Dim i As Integer
For Each param As OdbcParameter In cmd.Parameters
If Not (param.Direction = ParameterDirection.Output) AndAlso Not (param.Direction = ParameterDirection.ReturnValue) Then
param.Value = parameterValues(i)
_log.Info("Adding Parameter " & param.Value.ToString)
i += 1
End If
Next
End Sub
#End Region
#Region " Execute Stored Procedures "
''' <summary>
''' To Execute a SQL based Stored Prcedure that cannot be sent via a Transaction.
''' </summary>
''' <param name="spname">The name of the stored procedure to execute at the data source.</param>
''' <param name="parameterValues">The parameter values of the stored procedure.</param>
''' <returns>0 for Success or 1 for Failure</returns>
''' <remarks></remarks>
Public Function ExecuteSPSQL(ByVal spname As String, ByVal ParamArray parameterValues() As Object) As Integer
Dim command As SqlCommand = Nothing
Dim res As Integer = -1
Try
command = New SqlCommand(spname, MsSqlDBConn)
command.CommandType = CommandType.StoredProcedure
command.CommandTimeout = 0
SqlCommandBuilder.DeriveParameters(command)
_log.Info("Command Text : " & command.CommandText)
Me.AssignParameters(command, parameterValues)
_log.Info("Executing : " & command.CommandText)
res = command.ExecuteNonQuery()
Catch ex As Exception
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
Finally
If Not (command Is Nothing) Then command.Dispose()
End Try
Return res
End Function
#End Region
#Region " ExecuteNonQuery "
''' <summary>
''' Executes a Transact-SQL statement against the connection and returns the number of rows affected.
''' </summary>
''' <param name="cmd">The Transact-SQL statement or stored procedure to execute at the data source.</param>
''' <param name="cmdType">A value indicating how the System.Data.SqlClient.SqlCommand.CommandText property is to be interpreted.</param>
''' <param name="parameters">The parameters of the Transact-SQL statement or stored procedure.</param>
''' <returns>The number of rows affected.</returns>
Public Function ExecuteNonQuerySQL_NoTan(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As Integer
Dim transaction As SqlTransaction = Nothing
Dim command As SqlCommand = Nothing
Dim res As Integer = -1
Try
command = New SqlCommand(cmd, MsSqlDBConn)
command.CommandType = cmdType
command.CommandTimeout = 0
_log.Info("Command Text : " & command.CommandText)
Me.AssignParameters(command, parameters)
'transaction = MsSqlDBConn.BeginTransaction
'command.Transaction = transaction
_log.Info("Executing : " & command.CommandText)
res = command.ExecuteNonQuery()
Catch ex As Exception
If Not (transaction Is Nothing) Then
transaction.Rollback()
Throw New SqlDatabaseException("Transaction Rolled Back " & ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
End If
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
'Throw New SqlDatabaseException(ex.Message, ex.InnerException)
Finally
'transaction.Commit()
If Not (command Is Nothing) Then command.Dispose()
If Not (transaction Is Nothing) Then transaction.Dispose()
End Try
Return res
End Function
''' <summary>
''' Executes a Transact-SQL statement against the connection and returns the number of rows affected.
''' </summary>
''' <param name="cmd">The Transact-SQL statement or stored procedure to execute at the data source.</param>
''' <param name="cmdType">A value indicating how the System.Data.SqlClient.SqlCommand.CommandText property is to be interpreted.</param>
''' <param name="parameters">The parameters of the Transact-SQL statement or stored procedure.</param>
''' <returns>The number of rows affected.</returns>
Public Function ExecuteNonQuerySQL(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As Integer
Dim transaction As SqlTransaction = Nothing
Dim command As SqlCommand = Nothing
Dim res As Integer = -1
Try
command = New SqlCommand(cmd, MsSqlDBConn)
command.CommandType = cmdType
command.CommandTimeout = 0
_log.Info("Command Text : " & command.CommandText)
Me.AssignParameters(command, parameters)
transaction = MsSqlDBConn.BeginTransaction
command.Transaction = transaction
_log.Info("Executing : " & command.CommandText)
res = command.ExecuteNonQuery()
Catch ex As Exception
If Not (transaction Is Nothing) Then
transaction.Rollback()
Throw New SqlDatabaseException("Transaction Rolled Back " & ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
End If
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
Finally
transaction.Commit()
If Not (command Is Nothing) Then command.Dispose()
If Not (transaction Is Nothing) Then transaction.Dispose()
End Try
Return res
End Function
''' <summary>
''' Executes a Transact-SQL statement against the connection and returns the number of rows affected.
''' </summary>
''' <param name="spname">The stored procedure to execute at the data source.</param>
''' <param name="returnValue">The returned value from stored procedure.</param>
''' <param name="parameterValues">The parameter values of the stored procedure.</param>
''' <returns>The number of rows affected.</returns>
Public Function ExecuteNonQuerySQL(ByVal spname As String, ByRef returnValue As Integer, ByVal ParamArray parameterValues() As Object) As Integer
Dim transaction As SqlTransaction = Nothing
Dim command As SqlCommand = Nothing
Dim res As Integer = -1
Try
command = New SqlCommand(spname, MsSqlDBConn)
command.CommandType = CommandType.StoredProcedure
command.CommandTimeout = 0
SqlCommandBuilder.DeriveParameters(command)
_log.Info("Command Text : " & command.CommandText)
Me.AssignParameters(command, parameterValues)
transaction = MsSqlDBConn.BeginTransaction()
command.Transaction = transaction
_log.Info("Executing : " & command.CommandText)
res = command.ExecuteNonQuery()
returnValue = command.Parameters(0).Value
Catch ex As Exception
If Not (transaction Is Nothing) Then
transaction.Rollback()
Throw New SqlDatabaseException("Transaction Rolled Back " & ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
End If
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
Finally
transaction.Commit()
If Not (command Is Nothing) Then command.Dispose()
If Not (transaction Is Nothing) Then transaction.Dispose()
End Try
Return res
End Function
''' <summary>
''' Executes a Transact-SQL statement against the connection and returns the number of rows affected.
''' </summary>
''' <param name="cmd">The Transact-SQL statement or stored procedure to execute at the data source.</param>
''' <param name="cmdType">A value indicating how the System.Data.OdbcClient.OdbcCommand.CommandText property is to be interpreted.</param>
''' <param name="parameters">The parameters of the Transact-SQL statement or stored procedure.</param>
''' <returns>The number of rows affected.</returns>
Public Function ExecuteNonQueryODBC(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As OdbcParameter = Nothing) As Integer
Dim transaction As OdbcTransaction = Nothing
Dim command As OdbcCommand = Nothing
Dim res As Integer = -1
Try
command = New OdbcCommand(cmd, ODBCDBConn)
command.CommandType = cmdType
command.CommandTimeout = 0
_log.Info("Command Text : " & command.CommandText)
Me.AssignParameters(command, parameters)
transaction = ODBCDBConn.BeginTransaction
command.Transaction = transaction
_log.Info("Executing : " & command.CommandText)
res = command.ExecuteNonQuery()
Catch ex As Exception
If Not (transaction Is Nothing) Then
transaction.Rollback()
Throw New ApplicationException("Transaction Rolled Back " & ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
End If
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
Finally
transaction.Commit()
If Not (command Is Nothing) Then command.Dispose()
If Not (transaction Is Nothing) Then transaction.Dispose()
End Try
Return res
End Function
''' <summary>
''' Executes a Transact-SQL statement against the connection and returns the number of rows affected.
''' </summary>
''' <param name="spname">The stored procedure to execute at the data source.</param>
''' <param name="returnValue">The returned value from stored procedure.</param>
''' <param name="parameterValues">The parameter values of the stored procedure.</param>
''' <returns>The number of rows affected.</returns>
Public Function ExecuteNonQueryODBC(ByVal spname As String, ByRef returnValue As Integer, ByVal ParamArray parameterValues() As Object) As Integer
Dim transaction As OdbcTransaction = Nothing
Dim command As OdbcCommand = Nothing
Dim res As Integer = -1
Try
command = New OdbcCommand(spname, ODBCDBConn)
command.CommandType = CommandType.StoredProcedure
command.CommandTimeout = 0
_log.Info("Command Text : " & command.CommandText)
OdbcCommandBuilder.DeriveParameters(command)
Me.AssignParameters(command, parameterValues)
transaction = ODBCDBConn.BeginTransaction()
command.Transaction = transaction
_log.Info("Executing : " & command.CommandText)
res = command.ExecuteNonQuery()
returnValue = command.Parameters(0).Value
Catch ex As Exception
If Not (transaction Is Nothing) Then
transaction.Rollback()
End If
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
Throw New SqlDatabaseException("Transaction Rolled Back " & ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
Finally
transaction.Commit()
If Not (command Is Nothing) Then command.Dispose()
If Not (transaction Is Nothing) Then transaction.Dispose()
End Try
Return res
End Function
#End Region
#Region " ExecuteScalar "
''' <summary>
''' Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
''' </summary>
''' <param name="cmd">The Transact-SQL statement or stored procedure to execute at the data source.</param>
''' <param name="cmdType">A value indicating how the System.Data.SqlClient.SqlCommand.CommandText property is to be interpreted.</param>
''' <param name="parameters">The parameters of the Transact-SQL statement or stored procedure.</param>
''' <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
Public Function ExecuteScalarSQL(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As Object
Dim transaction As SqlTransaction = Nothing
Dim command As SqlCommand = Nothing
Dim res As Object = Nothing
Try
command = New SqlCommand(cmd, MsSqlDBConn)
command.CommandType = cmdType
command.CommandTimeout = 0
Me.AssignParameters(command, parameters)
transaction = MsSqlDBConn.BeginTransaction()
command.Transaction = transaction
res = command.ExecuteScalar()
transaction.Commit()
Catch ex As Exception
If Not (transaction Is Nothing) Then
transaction.Rollback()
End If
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
Finally
If Not (command Is Nothing) Then command.Dispose()
If Not (transaction Is Nothing) Then transaction.Dispose()
End Try
Return res
End Function
''' <summary>
''' Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
''' </summary>
''' <param name="spname">The stored procedure to execute at the data source.</param>
''' <param name="returnValue">The returned value from stored procedure.</param>
''' <param name="parameterValues">The parameter values of the stored procedure.</param>
''' <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
Public Function ExecuteScalarSQL(ByVal spname As String, ByRef returnValue As Integer, ByVal ParamArray parameterValues() As Object) As Object
Dim transaction As SqlTransaction = Nothing
Dim command As SqlCommand = Nothing
Dim res As Object = Nothing
Try
command = New SqlCommand(spname, MsSqlDBConn)
command.CommandType = CommandType.StoredProcedure
command.CommandTimeout = 0
SqlCommandBuilder.DeriveParameters(command)
Me.AssignParameters(command, parameterValues)
transaction = MsSqlDBConn.BeginTransaction()
command.Transaction = transaction
res = command.ExecuteScalar()
returnValue = command.Parameters(0).Value
transaction.Commit()
Catch ex As Exception
If Not (transaction Is Nothing) Then
transaction.Rollback()
End If
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
Finally
If Not (command Is Nothing) Then command.Dispose()
If Not (transaction Is Nothing) Then transaction.Dispose()
End Try
Return res
End Function
#End Region
#Region " ExecuteReader "
''' <summary>
''' Sends the System.Data.SqlClient.SqlCommand.CommandText to the System.Data.SqlClient.SqlCommand.Connection, and builds a System.Data.SqlClient.SqlDataReader using one of the System.Data.CommandBehavior values.
''' </summary>
''' <param name="cmd">The Transact-SQL statement or stored procedure to execute at the data source.</param>
''' <param name="cmdType">A value indicating how the System.Data.SqlClient.SqlCommand.CommandText property is to be interpreted.</param>
''' <param name="parameters">The parameters of the Transact-SQL statement or stored procedure.</param>
''' <returns>A System.Data.SqlClient.SqlDataReader object.</returns>
Public Function ExecuteReader(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As IDataReader
Dim command As SqlCommand = Nothing
Dim res As SqlDataReader = Nothing
Try
command = New SqlCommand(cmd, MsSqlDBConn)
command.CommandType = cmdType
command.CommandTimeout = 0
Me.AssignParameters(command, parameters)
res = command.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
End Try
Return CType(res, IDataReader)
End Function
''' <summary>
''' Sends the System.Data.SqlClient.SqlCommand.CommandText to the System.Data.SqlClient.SqlCommand.Connection, and builds a System.Data.SqlClient.SqlDataReader using one of the System.Data.CommandBehavior values.
''' </summary>
''' <param name="spname">The stored procedure to execute at the data source.</param>
''' <param name="returnValue">The returned value from stored procedure.</param>
''' <param name="parameterValues">The parameter values of the stored procedure.</param>
''' <returns>A System.Data.SqlClient.SqlDataReader object.</returns>
Public Function ExecuteReader(ByVal spname As String, ByRef returnValue As Integer, ByVal ParamArray parameterValues() As Object) As IDataReader
Dim connection As SqlConnection = Nothing
Dim command As SqlCommand = Nothing
Dim res As SqlDataReader = Nothing
Try
command = New SqlCommand(spname, connection)
command.CommandType = CommandType.StoredProcedure
command.CommandTimeout = 0
connection.Open()
SqlCommandBuilder.DeriveParameters(command)
Me.AssignParameters(command, parameterValues)
res = command.ExecuteReader(CommandBehavior.CloseConnection)
returnValue = command.Parameters(0).Value
Catch ex As Exception
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
End Try
Return CType(res, IDataReader)
End Function
#End Region
#Region " FillDataset "
''' <summary>
''' Adds or refreshes rows in the System.Data.DataSet to match those in the data source using the System.Data.DataSet name, and creates a System.Data.DataTable named "Table."
''' </summary>
''' <param name="cmd">The Transact-SQL statement or stored procedure to execute at the data source.</param>
''' <param name="cmdType">A value indicating how the System.Data.SqlClient.SqlCommand.CommandText property is to be interpreted.</param>
''' <param name="parameters">The parameters of the Transact-SQL statement or stored procedure.</param>
''' <returns>A System.Data.Dataset object.</returns>
Public Function FillDatasetSQL(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As DataSet
Dim command As SqlCommand = Nothing
Dim sqlda As SqlDataAdapter = Nothing
Dim res As New DataSet
Try
command = New SqlCommand(cmd, MsSqlDBConn)
command.CommandType = cmdType
command.CommandTimeout = 0
AssignParameters(command, parameters)
sqlda = New SqlDataAdapter(command)
sqlda.Fill(res)
Catch ex As Exception
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
Finally
If Not (command Is Nothing) Then command.Dispose()
If Not (sqlda Is Nothing) Then sqlda.Dispose()
End Try
Return res
End Function
''' <summary>
''' Adds or refreshes rows in the System.Data.DataSet to match those in the data source using the System.Data.DataSet name, and creates a System.Data.DataTable named "Table."
''' </summary>
''' <param name="cmd">The Transact-SQL statement or stored procedure to execute at the data source.</param>
''' <param name="cmdType">A value indicating how the System.Data.SqlClient.SqlCommand.CommandText property is to be interpreted.</param>
''' <param name="parameters">The parameters of the Transact-SQL statement or stored procedure.</param>
''' <returns>A System.Data.Dataset object.</returns>
Public Function FillDatasetODBC(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As OdbcParameter = Nothing) As DataSet
Dim command As OdbcCommand = Nothing
Dim sqlda As OdbcDataAdapter = Nothing
Dim res As New DataSet
Try
command = New OdbcCommand(cmd, ODBCDBConn)
command.CommandType = cmdType
command.CommandTimeout = 0
AssignParameters(command, parameters)
sqlda = New OdbcDataAdapter(command)
sqlda.Fill(res)
Catch ex As Exception
Throw New SqlDatabaseException(ex.Message, ex.InnerException)
_log.Error(ex.ToString)
_log.Error(ex.InnerException.ToString)
Finally
If Not (command Is Nothing) Then command.Dispose()
If Not (sqlda Is Nothing) Then sqlda.Dispose()
End Try
Return res
End Function
#End Region
#Region " ExecuteDataset "
''' <summary>
''' Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the System.Data.DataSet with the specified System.Data.DataTable name.
''' </summary>
''' <param name="insertCmd">A command used to insert new records into the data source.</param>
''' <param name="updateCmd">A command used to update records in the data source.</param>
''' <param name="deleteCmd">A command for deleting records from the data set.</param>
''' <param name="ds">The System.Data.DataSet to use to update the data source. </param>
''' <param name="srcTable">The name of the source table to use for table mapping.</param>
''' <returns>The number of rows successfully updated from the System.Data.DataSet.</returns>
Public Function ExecuteDataset(ByVal insertCmd As SqlCommand, ByVal updateCmd As SqlCommand, ByVal deleteCmd As SqlCommand, ByVal ds As DataSet, ByVal srcTable As String) As Integer
Dim sqlda As SqlDataAdapter = Nothing
Dim res As Integer = 0
Try
sqlda = New SqlDataAdapter
If Not (insertCmd Is Nothing) Then insertCmd.Connection = MsSqlDBConn : sqlda.InsertCommand = insertCmd
If Not (updateCmd Is Nothing) Then updateCmd.Connection = MsSqlDBConn : sqlda.UpdateCommand = updateCmd