Imports System.Data.SqlClient
Imports DigitalData.Modules.Logging
Public Class MSSQLServer
Public DBInitialized As Boolean = False
Public CurrentSQLConnectionString As String = ""
Private CurrentSQLConnection As SqlConnection
Private _Logger As Logger
Public Sub New(LogConfig As LogConfig, ConnectionString As String)
_Logger = LogConfig.GetLogger()
Try
Dim oSQLconnect As New SqlConnection
oSQLconnect.ConnectionString = ConnectionString
oSQLconnect.Open()
oSQLconnect.Close()
CurrentSQLConnectionString = ConnectionString
DBInitialized = True
Catch ex As Exception
DBInitialized = False
_Logger.Error(ex)
End Try
End Sub
Private Function GetSQLConnection()
Try
If IsNothing(CurrentSQLConnection) Then
Dim oSQLconnect As New SqlClient.SqlConnection
oSQLconnect.ConnectionString = CurrentSQLConnectionString
CurrentSQLConnection = oSQLconnect
CurrentSQLConnection.Open()
Else
If CurrentSQLConnection.State <> ConnectionState.Open Then
_Logger.Warn($"Actual ConnectionState is: '{CurrentSQLConnection.State.ToString}'")
Try
CurrentSQLConnection.Open()
Return True
Catch ex As Exception
_Logger.Warn("Could not reconnect to database!")
Return False
End Try
End If
End If
Return True
Catch ex As Exception
_Logger.Error(ex)
Return False
End Try
End Function
'''
''' Returns a datatable for a sql-statement
'''
''' sqlcommand for datatable (select XYZ from TableORView)
''' Optional Timeout
''' Returns a datatable
'''
Public Function GetDatatable(sqlcommand As String, Optional commandtimeout As Integer = 120) As DataTable
Try
Dim dt As DataTable = New DataTable()
If GetSQLConnection() = False Then
Return Nothing
End If
Dim oSQLCOmmand As SqlCommand
oSQLCOmmand = CurrentSQLConnection.CreateCommand()
oSQLCOmmand.CommandText = sqlcommand
oSQLCOmmand.CommandTimeout = commandtimeout
Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand)
adapter1.Fill(dt)
Return dt
Catch ex As Exception
_Logger.Error(ex)
_Logger.Debug("sqlcommand: " & sqlcommand)
Return Nothing
End Try
End Function
'''
''' Executes the passed sql-statement
'''
''' the sql statement
''' Optional Timeout
''' Returns true if properly executed, else false
'''
Public Function NewExecutenonQuery(executeStatement As String, Optional commandtimeout As Integer = 120) As Boolean
Try
If GetSQLConnection() = False Then
Return Nothing
End If
'Dim oSQLconnect As New SqlClient.SqlConnection
Dim oSQLCOmmand As SqlCommand
oSQLCOmmand = CurrentSQLConnection.CreateCommand()
oSQLCOmmand.CommandText = executeStatement
oSQLCOmmand.CommandTimeout = commandtimeout
oSQLCOmmand.ExecuteNonQuery()
oSQLCOmmand.Dispose()
Return True
Catch ex As Exception
_Logger.Error(ex)
_Logger.Debug("executeStatement: " & executeStatement)
Return False
End Try
End Function
'''
''' Executes the passed sql-statement in asyncmode
'''
''' the sql statement
''' Optional Timeout
'''
Public Sub NewExecuteNonQueryAsync(executeStatement As String, Optional commandtimeout As Integer = 120)
If GetSQLConnection() = False Then
Exit Sub
End If
Dim oSQLCOmmand As SqlCommand
Dim callback As New AsyncCallback(AddressOf NewExecuteNonQueryAsync_Callback)
Try
oSQLCOmmand = CurrentSQLConnection.CreateCommand()
oSQLCOmmand.CommandText = executeStatement
oSQLCOmmand.CommandTimeout = commandtimeout
oSQLCOmmand.BeginExecuteNonQuery(callback, oSQLCOmmand)
oSQLCOmmand.Dispose()
Catch ex As Exception
_Logger.Error(ex)
_Logger.Debug("executeStatement: " & executeStatement)
End Try
End Sub
Private Sub NewExecuteNonQueryAsync_Callback(ByVal result As IAsyncResult)
Dim command As SqlCommand = CType(result.AsyncState, SqlCommand)
Dim res = command.EndExecuteNonQuery(result)
_Logger.Info(String.Format("Finished executing Async database operation: {0}", command.CommandText))
End Sub
'''
''' Executes the passed sql-statement as Scalar
'''
''' the sql statement
''' Optional Timeout
''' Returns true if properly executed, else false
'''
Public Function NewExecuteScalar(executeStatement As String, Optional commandtimeout As Integer = 120)
Dim result
Try
If GetSQLConnection() = False Then
Return Nothing
End If
Dim oSQLCOmmand As SqlClient.SqlCommand
oSQLCOmmand = CurrentSQLConnection.CreateCommand()
oSQLCOmmand.CommandText = executeStatement
oSQLCOmmand.CommandTimeout = commandtimeout
result = oSQLCOmmand.ExecuteScalar()
oSQLCOmmand.Dispose()
Return result
Catch ex As Exception
_Logger.Error(ex)
_Logger.Debug("executeStatement: " & executeStatement)
Return Nothing
End Try
End Function
End Class