408 lines
17 KiB
VB.net
408 lines
17 KiB
VB.net
Imports System.Data.Common
|
|
Imports System.Data.SqlClient
|
|
Imports DigitalData.Modules.Logging
|
|
|
|
Public Class MSSQLServer
|
|
Implements IDatabase
|
|
|
|
Public DBInitialized As Boolean = False
|
|
Public CurrentSQLConnectionString As String = ""
|
|
|
|
Private ReadOnly _Timeout As Integer
|
|
Private ReadOnly _Logger As Logger
|
|
|
|
Public Sub New(LogConfig As LogConfig, ConnectionString As String, Optional Timeout As Integer = 120)
|
|
_Logger = LogConfig.GetLogger()
|
|
_Timeout = Timeout
|
|
|
|
CurrentSQLConnectionString = ConnectionString
|
|
|
|
Try
|
|
DBInitialized = TestCanConnect()
|
|
Catch ex As Exception
|
|
DBInitialized = False
|
|
_Logger.Error(ex)
|
|
End Try
|
|
End Sub
|
|
|
|
Public Sub New(LogConfig As LogConfig, Server As String, Database As String, UserId As String, Password As String, Optional Timeout As Integer = 120)
|
|
_Logger = LogConfig.GetLogger()
|
|
_Timeout = Timeout
|
|
|
|
CurrentSQLConnectionString = GetConnectionString(Server, Database, UserId, Password)
|
|
|
|
Try
|
|
DBInitialized = TestCanConnect()
|
|
Catch ex As Exception
|
|
DBInitialized = False
|
|
_Logger.Error(ex)
|
|
End Try
|
|
End Sub
|
|
|
|
Public Function GetConnectionString(Server As String, Database As String, UserId As String, Password As String) As String
|
|
Dim oConnectionStringBuilder As New SqlConnectionStringBuilder() With {
|
|
.DataSource = Server,
|
|
.InitialCatalog = Database,
|
|
.UserID = UserId,
|
|
.Password = Password
|
|
}
|
|
|
|
Return oConnectionStringBuilder.ToString
|
|
End Function
|
|
Public Function Get_ConnectionStringforID(pID As Integer)
|
|
Dim connectionString As String = ""
|
|
Try
|
|
'Me.TBCONNECTIONTableAdapter.FillByID(Me.DD_DMSLiteDataSet.TBCONNECTION, id)
|
|
Dim oDTConnection As DataTable = GetDatatable("SELECT * FROM TBDD_CONNECTION WHERE GUID = " & pID)
|
|
If oDTConnection.Rows.Count = 1 Then
|
|
Select Case oDTConnection.Rows(0).Item("SQL_PROVIDER").ToString.ToUpper
|
|
Case "MS-SQL"
|
|
If oDTConnection.Rows(0).Item("USERNAME") = "WINAUTH" Then
|
|
connectionString = "Server=" & oDTConnection.Rows(0).Item("SERVER") & ";Database=" & oDTConnection.Rows(0).Item("DATENBANK") & ";Trusted_Connection=True;"
|
|
Else
|
|
connectionString = "Server=" & oDTConnection.Rows(0).Item("SERVER") & ";Database=" & oDTConnection.Rows(0).Item("DATENBANK") & ";User Id=" & oDTConnection.Rows(0).Item("USERNAME") & ";Password=" & oDTConnection.Rows(0).Item("USERNAME") & ";Password=" & oDTConnection.Rows(0).Item("PASSWORD") & ";"
|
|
End If
|
|
' connectionString = "Server=" & DTConnection.Rows(0).Item("SERVER") & ";Database=" & DTConnection.Rows(0).Item("DATENBANK") & ";User Id=" & DTConnection.Rows(0).Item("USERNAME") & ";Password=" & DTConnection.Rows(0).Item("PASSWORD") & ";"
|
|
Case "Oracle"
|
|
If oDTConnection.Rows(0).Item("BEMERKUNG").ToString.Contains("without tnsnames") Then
|
|
connectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" & oDTConnection.Rows(0).Item("SERVER") & ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" &
|
|
oDTConnection.Rows(0).Item("DATENBANK") & ")));User Id=" & oDTConnection.Rows(0).Item("USERNAME") & ";Password=" & oDTConnection.Rows(0).Item("PASSWORD") & ";"
|
|
Else
|
|
connectionString = "Data Source=" & oDTConnection.Rows(0).Item("SERVER") & ";Persist Security Info=True;User Id=" & oDTConnection.Rows(0).Item("USERNAME") & ";Password=" & oDTConnection.Rows(0).Item("PASSWORD") & ";Unicode=True"
|
|
End If
|
|
'Case "ODBC"
|
|
' Dim conn As New OdbcConnection("dsn=" & DTConnection.Rows(0).Item("SERVER") & ";uid=" & DTConnection.Rows(0).Item("USERNAME") & ";pwd=" + DTConnection.Rows(0).Item("PASSWORD"))
|
|
' connectionString = conn.ConnectionString
|
|
Case Else
|
|
_Logger.Info(" - ConnectionType nicht integriert")
|
|
|
|
End Select
|
|
Else
|
|
_Logger.Info(" No entry for Connection-ID: " & pID.ToString)
|
|
End If
|
|
|
|
Catch ex As Exception
|
|
_Logger.Error(ex)
|
|
_Logger.Info(" - Error in bei Get ConnectionString - Fehler: " & vbNewLine & ex.Message)
|
|
End Try
|
|
Return connectionString
|
|
End Function
|
|
|
|
Private Function TestCanConnect() As Boolean
|
|
Return TestCanConnect(CurrentSQLConnectionString)
|
|
End Function
|
|
|
|
Private Function TestCanConnect(ConnectionString As String) As Boolean
|
|
Try
|
|
Dim oConnection As New SqlConnection(ConnectionString)
|
|
oConnection.Open()
|
|
oConnection.Close()
|
|
Return True
|
|
Catch ex As Exception
|
|
_Logger.Error(ex)
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
Private Function GetSQLConnection() As SqlConnection
|
|
Return GetSQLConnection(CurrentSQLConnectionString)
|
|
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
|
|
|
|
''' <summary>
|
|
''' Returns a datatable for a sql-statement
|
|
''' </summary>
|
|
''' <param name="SqlCommand">sqlcommand for datatable (select XYZ from TableORView)</param>
|
|
''' <returns>Returns a datatable</returns>
|
|
Public Function GetDatatable(SqlCommand As String, Timeout As Integer) As DataTable Implements IDatabase.GetDatatable
|
|
Try
|
|
If TestCanConnect() = False Then
|
|
Return Nothing
|
|
End If
|
|
|
|
_Logger.Debug("GetDatatable: Running Query [{0}]", SqlCommand)
|
|
|
|
Using oConnection = GetSQLConnection()
|
|
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.Warn($"GetDatatable failed SQLCommand [{SqlCommand}] - ERROR: {ex.Message}")
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Public Function GetDatatable(SqlCommand As String) As DataTable Implements IDatabase.GetDatatable
|
|
Return GetDatatable(SqlCommand, _Timeout)
|
|
End Function
|
|
|
|
Public Async Function GetDatatableAsync(SqlCommand As String) As Task(Of DataTable)
|
|
Return Await Task.Run(Function()
|
|
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("GetDatatableWithConnection: 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.Warn($"GetDatatableWithConnection 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
|
|
Try
|
|
If TestCanConnect() = False Then
|
|
Return Nothing
|
|
End If
|
|
|
|
_Logger.Debug("ExecuteNonQuery: Running Query [{0}]", SQLCommand)
|
|
|
|
Using oConnection = GetSQLConnection()
|
|
Using oSQLCOmmand = oConnection.CreateCommand()
|
|
oSQLCOmmand.CommandText = SQLCommand
|
|
oSQLCOmmand.CommandTimeout = 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
|
|
Public Function ExecuteNonQueryCS(pSQLCommand As String, ConnString As String, Optional pInfo As String = "")
|
|
Try
|
|
If pInfo <> "" Then
|
|
pInfo = "[" & pInfo & "]"
|
|
End If
|
|
Dim SQLconnect As New SqlClient.SqlConnection
|
|
Dim SQLcommand As SqlClient.SqlCommand
|
|
SQLconnect.ConnectionString = ConnString
|
|
SQLconnect.Open()
|
|
SQLcommand = SQLconnect.CreateCommand
|
|
'Update Last Created Record in Foo
|
|
SQLcommand.CommandText = pSQLCommand
|
|
_Logger.Debug("Execute_non_Query_ConStr Created: " & pSQLCommand)
|
|
SQLcommand.ExecuteNonQuery()
|
|
SQLcommand.Dispose()
|
|
SQLconnect.Close()
|
|
Return True
|
|
Catch ex As Exception
|
|
_Logger.Warn($"ExecuteNonQueryCS failed SQLCommand [{pSQLCommand}] - ERROR: {ex.Message}")
|
|
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
Public Function ExecuteNonQuery(SQLCommand As String) As Boolean Implements IDatabase.ExecuteNonQuery
|
|
Return ExecuteNonQuery(SQLCommand, _Timeout)
|
|
End Function
|
|
|
|
Public Async Function ExecuteNonQueryAsync(SQLCommand As String) As Task(Of Boolean)
|
|
Return Await Task.Run(Function()
|
|
Return ExecuteNonQuery(SQLCommand, _Timeout)
|
|
End Function)
|
|
End Function
|
|
|
|
Public Function GetScalarValue(SQLCommand As String, Timeout As Integer) As Object Implements IDatabase.GetScalarValue
|
|
Try
|
|
If TestCanConnect() = 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.Warn($"GetScalarValue failed SQLCommand [{SQLCommand}] - ERROR: {ex.Message}")
|
|
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Public Function GetScalarValue(SQLQuery As String) As Object Implements IDatabase.GetScalarValue
|
|
Return GetScalarValue(SQLQuery, _Timeout)
|
|
End Function
|
|
|
|
Public Async Function GetScalarValueAsync(SQLQuery As String) As Task(Of Object)
|
|
Return Await Task.Run(Function()
|
|
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(ConnectionString)
|
|
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.Warn($"GetScalarValueWithConnection failed SQLCommand [{SQLCommand}] - ERROR: {ex.Message}")
|
|
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
|
|
Return Nothing
|
|
End If
|
|
|
|
_Logger.Debug("GetScalarValue: Running Query [{0}]", SQLCommand)
|
|
|
|
If SQLCommand.CommandText.Contains(" ") Then
|
|
SQLCommand.CommandType = CommandType.Text
|
|
Else
|
|
SQLCommand.CommandType = CommandType.StoredProcedure
|
|
End If
|
|
|
|
Using oConnection As SqlConnection = GetSQLConnection()
|
|
|
|
SQLCommand.Connection = oConnection
|
|
SQLCommand.Parameters(OutputParameter).Direction = ParameterDirection.Output
|
|
SQLCommand.CommandTimeout = Timeout
|
|
SQLCommand.ExecuteNonQuery()
|
|
oConnection.Close()
|
|
|
|
Return SQLCommand.Parameters(OutputParameter).Value
|
|
End Using
|
|
Catch ex As Exception
|
|
_Logger.Warn($"GetScalarValue failed SQLCommand [{SQLCommand}] - ERROR: {ex.Message}")
|
|
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
Public Function GetScalarValueConStr(pSQLCommand As String, ConString As String, Optional pInfo As String = "")
|
|
Dim result
|
|
Try
|
|
If pInfo <> "" Then
|
|
pInfo = "[" & pInfo & "]"
|
|
End If
|
|
Dim SQLconnect As New SqlClient.SqlConnection
|
|
Dim SQLcommand As SqlClient.SqlCommand
|
|
SQLconnect.ConnectionString = ConString
|
|
SQLconnect.Open()
|
|
SQLcommand = SQLconnect.CreateCommand
|
|
'Update Last Created Record in Foo
|
|
SQLcommand.CommandText = pSQLCommand
|
|
_Logger.Debug("Execute_Scalar_ConStr Scalar: " & pSQLCommand)
|
|
result = SQLcommand.ExecuteScalar()
|
|
SQLcommand.Dispose()
|
|
SQLconnect.Close()
|
|
Return result
|
|
Catch ex As Exception
|
|
_Logger.Warn($"GetScalarValueConStr failed SQLCommand [{pSQLCommand}] - ERROR: {ex.Message}")
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Public Function GetScalarValue(SQLCommand As SqlCommand, OutputParameter As String) As Object
|
|
Return GetScalarValue(SQLCommand, OutputParameter, _Timeout)
|
|
End Function
|
|
|
|
''' <summary>
|
|
''' Executes the passed sql-statement in asyncmode
|
|
''' </summary>
|
|
''' <param name="SqlCommand">the sql statement</param>
|
|
''' <param name="commandtimeout">Optional Timeout</param>
|
|
''' <remarks></remarks>
|
|
Public Sub NewExecuteNonQueryAsync(SqlCommand As String, Optional commandtimeout As Integer = 120)
|
|
If TestCanConnect() = False Then
|
|
Exit Sub
|
|
End If
|
|
|
|
_Logger.Debug("NewExecuteNonQueryAsync: Running Query [{0}]", SqlCommand)
|
|
|
|
Try
|
|
Dim oCallback As New AsyncCallback(AddressOf NewExecuteNonQueryAsync_Callback)
|
|
|
|
Using oConnection As SqlConnection = GetSQLConnection()
|
|
Using oSQLCOmmand = oConnection.CreateCommand()
|
|
oSQLCOmmand.CommandText = SqlCommand
|
|
oSQLCOmmand.CommandTimeout = commandtimeout
|
|
oSQLCOmmand.BeginExecuteNonQuery(oCallback, oSQLCOmmand)
|
|
End Using
|
|
End Using
|
|
Catch ex As Exception
|
|
_Logger.Warn($"NewExecuteNonQueryAsync failed SQLCommand [{SqlCommand}] - ERROR: {ex.Message}")
|
|
|
|
End Try
|
|
End Sub
|
|
|
|
Private Sub NewExecuteNonQueryAsync_Callback(ByVal result As IAsyncResult)
|
|
Dim command As SqlCommand = CType(result.AsyncState, SqlCommand)
|
|
Dim res = command.EndExecuteNonQuery(result)
|
|
_Logger.Info("Finished executing Async database operation: {0}", command.CommandText)
|
|
End Sub
|
|
End Class
|