2020-09-22 10:50:20 +02:00

112 lines
3.9 KiB
VB.net

Imports System.Data.SqlClient
Public Class Database
Private _Config As ConfigManager(Of Config)
Private _LogConfig As LogConfig
Private _Logger As Logger
Private _ConnectionString As String = ""
Private Const QUERY_TIMEOUT = 120000
Public Sub New(LogConfig As LogConfig, ConfigManager As ConfigManager(Of Config), ConnectionString As String)
_LogConfig = LogConfig
_Logger = LogConfig.GetLogger()
_Config = ConfigManager
_ConnectionString = ConnectionString
End Sub
#Region "Database-Access"
Private Function GetSQLConnection() As SqlConnection
Return GetSQLConnection(_ConnectionString)
End Function
Private Function GetSQLConnection(ConnectionString As String) As SqlConnection
Try
Dim oConnection As New SqlConnection(ConnectionString)
oConnection.Open()
Dim oMaskedConnectionString = MaskConnectionString(ConnectionString)
_Logger.Debug("The Following Connection is open: {0}", oMaskedConnectionString)
Return oConnection
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function MaskConnectionString(ConnectionString As String) As String
Try
Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = ConnectionString}
Dim oConnectionString = ConnectionString.Replace(oBuilder.Password, "XXXXX")
Return oConnectionString
Catch ex As Exception
_Logger.Error(ex)
Return "Invalid ConnectionString"
End Try
End Function
Public Function GetDatatable(SqlCommand As String) As DataTable
Try
_Logger.Debug("GetDatatable: Running Query [{0}]", SqlCommand)
Using oConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SqlCommand
oSQLCOmmand.CommandTimeout = QUERY_TIMEOUT
Dim dt As DataTable = New DataTable()
Dim oAdapter As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand)
oAdapter.Fill(dt)
Return dt
End Using
End Using
Catch ex As Exception
_Logger.Warn($"GetDatatable failed SQLCommand [{SqlCommand}] - ERROR: {ex.Message}")
Return Nothing
End Try
End Function
Public Function GetScalarValue(SQLCommand As String) As Object
Try
_Logger.Debug("GetScalarValue: Running Query [{0}]", SQLCommand)
Using oConnection As SqlConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SQLCommand
oSQLCOmmand.CommandTimeout = QUERY_TIMEOUT
Dim oResult As Object = oSQLCOmmand.ExecuteScalar()
Return oResult
End Using
End Using
Catch ex As Exception
_Logger.Warn($"GetScalarValue failed SQLCommand [{SQLCommand}] - ERROR: {ex.Message}")
Return Nothing
End Try
End Function
Public Function ExecuteNonQuery(SQLCommand As String) As Boolean
Try
_Logger.Debug("ExecuteNonQuery: Running Query [{0}]", SQLCommand)
Using oConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SQLCommand
oSQLCOmmand.CommandTimeout = QUERY_TIMEOUT
oSQLCOmmand.ExecuteNonQuery()
Return True
End Using
End Using
Catch ex As Exception
_Logger.Warn($"ExecuteNonQuery failed SQLCommand [{SQLCommand}] - ERROR: {ex.Message}")
Return False
End Try
End Function
#End Region
End Class