Database: Add GetScalarValueWithConnection

This commit is contained in:
Jonathan Jenne 2020-06-16 15:36:37 +02:00
parent b7c17250eb
commit d57be5ab39

View File

@ -72,15 +72,11 @@ Public Class MSSQLServer
Private Function GetSQLConnection(ConnectionString As String) As SqlConnection Private Function GetSQLConnection(ConnectionString As String) As SqlConnection
Try Try
Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = ConnectionString}
Dim oConnection As New SqlConnection(ConnectionString) Dim oConnection As New SqlConnection(ConnectionString)
oConnection.Open() oConnection.Open()
Try Dim oMaskedConnectionString = MaskConnectionString(ConnectionString)
Dim oConnectionString = ConnectionString.Replace(oBuilder.Password, "XXXXX") _Logger.Debug("The Following Connection is open: {0}", oMaskedConnectionString)
_Logger.Debug($"Following Connectionstring is open: {oConnectionString}")
Catch ex As Exception
End Try
Return oConnection Return oConnection
Catch ex As Exception Catch ex As Exception
@ -90,6 +86,17 @@ Public Class MSSQLServer
End Try End Try
End Function 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
''' <summary> ''' <summary>
''' Returns a datatable for a sql-statement ''' Returns a datatable for a sql-statement
''' </summary> ''' </summary>
@ -146,7 +153,9 @@ Public Class MSSQLServer
End Using End Using
Catch ex As Exception Catch ex As Exception
_Logger.Error(ex) _Logger.Error(ex)
_Logger.Warn("sqlcommand: " & SqlCommand) _Logger.Warn("GetDatatableWithConnection failed with:")
_Logger.Warn("ConnectionString: ")
_Logger.Warn("SQL Query: " & SqlCommand)
Return Nothing Return Nothing
End Try End Try
End Function End Function
@ -168,7 +177,7 @@ Public Class MSSQLServer
Return Nothing Return Nothing
End If End If
_Logger.Debug("Running Query: {0}", SQLCommand) _Logger.Debug("ExecuteNonQuery: Running Query [{0}]", SQLCommand)
Using oConnection = GetSQLConnection() Using oConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand() Using oSQLCOmmand = oConnection.CreateCommand()
@ -227,7 +236,7 @@ Public Class MSSQLServer
Return Nothing Return Nothing
End If End If
_Logger.Debug("Running Query: {0}", SQLCommand) _Logger.Debug("GetScalarValue: Running Query [{0}]", SQLCommand)
Using oConnection As SqlConnection = GetSQLConnection() Using oConnection As SqlConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand() Using oSQLCOmmand = oConnection.CreateCommand()
@ -248,6 +257,29 @@ Public Class MSSQLServer
Return GetScalarValue(SQLQuery, _Timeout) Return GetScalarValue(SQLQuery, _Timeout)
End Function End Function
Public Function GetScalarValueWithConnection(SQLCommand As String, ConnectionString As String) As Object
Try
If TestCanConnect(ConnectionString) = False Then
Return Nothing
End If
_Logger.Debug("GetScalarValue: Running Query [{0}]", SQLCommand)
Using oConnection As SqlConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SQLCommand
oSQLCOmmand.CommandTimeout = _Timeout
Dim oResult As Object = oSQLCOmmand.ExecuteScalar()
Return oResult
End Using
End Using
Catch ex As Exception
_Logger.Error(ex)
_Logger.Warn("SQLQuery: " & SQLCommand)
Return Nothing
End Try
End Function
Public Function GetScalarValue(SQLCommand As SqlCommand, OutputParameter As String, Timeout As Integer) As Object Public Function GetScalarValue(SQLCommand As SqlCommand, OutputParameter As String, Timeout As Integer) As Object
Try Try
If TestCanConnect() = False Then If TestCanConnect() = False Then