Modules.Database
This commit is contained in:
166
Modules.Database/MSSQLServer.vb
Normal file
166
Modules.Database/MSSQLServer.vb
Normal file
@@ -0,0 +1,166 @@
|
||||
Imports System.Data.SqlClient
|
||||
|
||||
Public Class MSSQLServer
|
||||
Private Shared Logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger
|
||||
Public DBInitialized As Boolean = False
|
||||
Public CurrentSQLConnectionString As String = ""
|
||||
Private CurrentSQLConnection As SqlClient.SqlConnection
|
||||
Public Sub New(CONSTRING As String)
|
||||
Init(CONSTRING)
|
||||
End Sub
|
||||
Public Function Init(CONSTRING As String)
|
||||
Try
|
||||
Dim oSQLconnect As New SqlClient.SqlConnection
|
||||
oSQLconnect.ConnectionString = CONSTRING
|
||||
oSQLconnect.Open()
|
||||
oSQLconnect.Close()
|
||||
CurrentSQLConnectionString = CONSTRING
|
||||
DBInitialized = True
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
DBInitialized = False
|
||||
Logger.Error(ex)
|
||||
'clsLogger.Add("Error in DatabaseInit: " & ex.Message, True)
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
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
|
||||
''' <summary>
|
||||
''' Returns a datatable for a sql-statement
|
||||
''' </summary>
|
||||
''' <param name="sqlcommand">sqlcommand for datatable (select XYZ from TableORView)</param>
|
||||
''' <param name="commandtimeout">Optional Timeout</param>
|
||||
''' <returns>Returns a datatable</returns>
|
||||
''' <remarks></remarks>
|
||||
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 SqlClient.SqlCommand
|
||||
|
||||
oSQLCOmmand = CurrentSQLConnection.CreateCommand()
|
||||
oSQLCOmmand.CommandText = sqlcommand
|
||||
oSQLCOmmand.CommandTimeout = commandtimeout
|
||||
Dim adapter1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(oSQLCOmmand)
|
||||
adapter1.Fill(dt)
|
||||
|
||||
Return dt
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Logger.Debug("sqlcommand: " & sqlcommand)
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
''' <summary>
|
||||
''' Executes the passed sql-statement
|
||||
''' </summary>
|
||||
''' <param name="executeStatement">the sql statement</param>
|
||||
''' <param name="commandtimeout">Optional Timeout</param>
|
||||
''' <returns>Returns true if properly executed, else false</returns>
|
||||
''' <remarks></remarks>
|
||||
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 SqlClient.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
|
||||
''' <summary>
|
||||
''' Executes the passed sql-statement in asyncmode
|
||||
''' </summary>
|
||||
''' <param name="executeStatement">the sql statement</param>
|
||||
''' <param name="commandtimeout">Optional Timeout</param>
|
||||
''' <remarks></remarks>
|
||||
Public Sub NewExecuteNonQueryAsync(executeStatement As String, Optional commandtimeout As Integer = 120)
|
||||
If GetSQLConnection() = False Then
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Dim oSQLCOmmand As SqlClient.SqlCommand
|
||||
Dim callback As New AsyncCallback(AddressOf Execute_non_Query_Async_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 Execute_non_Query_Async_Callback(ByVal result As IAsyncResult)
|
||||
Dim command As SqlClient.SqlCommand = CType(result.AsyncState, SqlClient.SqlCommand)
|
||||
Dim res = command.EndExecuteNonQuery(result)
|
||||
Logger.Info(String.Format("Finished executing Async database operation: {0}", command.CommandText))
|
||||
End Sub
|
||||
''' <summary>
|
||||
''' Executes the passed sql-statement as Scalar
|
||||
''' </summary>
|
||||
''' <param name="executeStatement">the sql statement</param>
|
||||
''' <param name="commandtimeout">Optional Timeout</param>
|
||||
''' <returns>Returns true if properly executed, else false</returns>
|
||||
''' <remarks></remarks>
|
||||
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
|
||||
Reference in New Issue
Block a user