Database: Add Alternate GetDatatableWithConnection2

This commit is contained in:
Jonathan Jenne 2021-02-10 10:49:31 +01:00
parent cc14575ed2
commit 45a132715b

View File

@ -201,6 +201,52 @@ Public Class MSSQLServer
End Try End Try
End Function End Function
Public Function GetDatatableWithConnection2(SqlCommand As String, ConnectionString As String) As DataTable
Try
If TestCanConnect(ConnectionString) = False Then
Return Nothing
End If
_Logger.Debug("GetDatatableWithConnection2: Running Query [{0}]", SqlCommand)
Using oConnection = GetSQLConnection(ConnectionString)
Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SqlCommand
oSQLCOmmand.CommandTimeout = _Timeout
Dim oTable As DataTable = New DataTable("DD_RESULT")
Using oReader As SqlDataReader = oSQLCOmmand.ExecuteReader()
Dim oSchemaTable As DataTable = oReader.GetSchemaTable()
For Each oRow As DataRow In oSchemaTable.Rows
Dim oDataColumn As New DataColumn
oDataColumn.ColumnName = oRow("ColumnName").ToString()
oDataColumn.DataType = Type.GetType(oRow("DataType").ToString())
oTable.Columns.Add(oDataColumn)
Next
While (oReader.Read())
Dim oRow As DataRow = oTable.NewRow()
For index = 0 To oTable.Columns.Count - 1
oRow.Item(index) = oReader.Item(index)
Next
oTable.Rows.Add(oRow)
End While
Return oTable
End Using
End Using
End Using
Catch ex As Exception
_Logger.Warn($"GetDatatableWithConnection2 failed SQLCommand [{SqlCommand}] - ERROR: {ex.Message}")
Return Nothing
End Try
End Function
Public Function ExecuteNonQuery(SQLCommand As String, Timeout As Integer) As Boolean Implements IDatabase.ExecuteNonQuery Public Function ExecuteNonQuery(SQLCommand As String, Timeout As Integer) As Boolean Implements IDatabase.ExecuteNonQuery
Try Try
If TestCanConnect() = False Then If TestCanConnect() = False Then