Database/MSSQL: Support connection string per query, for now only for datatable
This commit is contained in:
parent
e61f85c4cc
commit
21802dcfa2
@ -51,8 +51,12 @@ Public Class MSSQLServer
|
|||||||
End Function
|
End Function
|
||||||
|
|
||||||
Private Function TestCanConnect() As Boolean
|
Private Function TestCanConnect() As Boolean
|
||||||
|
Return TestCanConnect(CurrentSQLConnectionString)
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Private Function TestCanConnect(ConnectionString As String) As Boolean
|
||||||
Try
|
Try
|
||||||
Dim oConnection As New SqlConnection(CurrentSQLConnectionString)
|
Dim oConnection As New SqlConnection(ConnectionString)
|
||||||
oConnection.Open()
|
oConnection.Open()
|
||||||
oConnection.Close()
|
oConnection.Close()
|
||||||
Return True
|
Return True
|
||||||
@ -63,13 +67,17 @@ Public Class MSSQLServer
|
|||||||
End Function
|
End Function
|
||||||
|
|
||||||
Private Function GetSQLConnection() As SqlConnection
|
Private Function GetSQLConnection() As SqlConnection
|
||||||
|
Return GetSQLConnection(CurrentSQLConnectionString)
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Private Function GetSQLConnection(ConnectionString As String) As SqlConnection
|
||||||
Try
|
Try
|
||||||
Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = CurrentSQLConnectionString}
|
Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = ConnectionString}
|
||||||
Dim oConnection As New SqlConnection(CurrentSQLConnectionString)
|
Dim oConnection As New SqlConnection(ConnectionString)
|
||||||
oConnection.Open()
|
oConnection.Open()
|
||||||
|
|
||||||
Try
|
Try
|
||||||
Dim oConnectionString = CurrentSQLConnectionString.Replace(oBuilder.Password, "XXXXX")
|
Dim oConnectionString = ConnectionString.Replace(oBuilder.Password, "XXXXX")
|
||||||
_Logger.Debug($"Following Connectionstring is open: {oConnectionString}")
|
_Logger.Debug($"Following Connectionstring is open: {oConnectionString}")
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
End Try
|
End Try
|
||||||
@ -117,6 +125,32 @@ Public Class MSSQLServer
|
|||||||
Return GetDatatable(SqlCommand, _Timeout)
|
Return GetDatatable(SqlCommand, _Timeout)
|
||||||
End Function
|
End Function
|
||||||
|
|
||||||
|
Public Function GetDatatableWithConnection(SqlCommand As String, ConnectionString As String) As DataTable
|
||||||
|
Try
|
||||||
|
If TestCanConnect(ConnectionString) = False Then
|
||||||
|
Return Nothing
|
||||||
|
End If
|
||||||
|
|
||||||
|
_Logger.Debug("Running Query: {0}", SqlCommand)
|
||||||
|
|
||||||
|
Using oConnection = GetSQLConnection(ConnectionString)
|
||||||
|
Using oSQLCOmmand = oConnection.CreateCommand()
|
||||||
|
oSQLCOmmand.CommandText = SqlCommand
|
||||||
|
oSQLCOmmand.CommandTimeout = _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.Error(ex)
|
||||||
|
_Logger.Warn("sqlcommand: " & SqlCommand)
|
||||||
|
Return Nothing
|
||||||
|
End Try
|
||||||
|
End Function
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' Executes the passed sql-statement
|
''' Executes the passed sql-statement
|
||||||
''' </summary>
|
''' </summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user