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