From f418074011c742415c2f07abdf075dc5d4543d25 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 6 Aug 2019 15:56:30 +0200 Subject: [PATCH] Support for ODBC --- Modules.Database/Database.vbproj | 1 + Modules.Database/Firebird.vb | 3 +- Modules.Database/MSSQLServer.vb | 17 ++- Modules.Database/ODBC.vb | 179 +++++++++++++++++++++++++++++++ 4 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 Modules.Database/ODBC.vb diff --git a/Modules.Database/Database.vbproj b/Modules.Database/Database.vbproj index e7c4b685..1549b877 100644 --- a/Modules.Database/Database.vbproj +++ b/Modules.Database/Database.vbproj @@ -92,6 +92,7 @@ + diff --git a/Modules.Database/Firebird.vb b/Modules.Database/Firebird.vb index 1bc2b3b2..14f89eb6 100644 --- a/Modules.Database/Firebird.vb +++ b/Modules.Database/Firebird.vb @@ -124,8 +124,7 @@ Public Class Firebird Return oConnection Catch ex As Exception - _logger.Error(ex) - + _Logger.Error(ex) Return Nothing End Try End Function diff --git a/Modules.Database/MSSQLServer.vb b/Modules.Database/MSSQLServer.vb index aff2870e..139a13f0 100644 --- a/Modules.Database/MSSQLServer.vb +++ b/Modules.Database/MSSQLServer.vb @@ -79,6 +79,11 @@ Public Class MSSQLServer ''' Returns true if properly executed, else false ''' Public Function NewExecutenonQuery(executeStatement As String, Optional Timeout As Integer = 120) As Boolean + _Logger.Warn("NewExecutenonQuery is deprecated. Use ExecuteNonQuery instead.") + Return ExecuteNonQuery(executeStatement, Timeout) + End Function + + Public Function ExecuteNonQuery(SQLCommand As String, Optional Timeout As Integer = 120) As Boolean Try If TestCanConnect() = False Then Return Nothing @@ -86,7 +91,7 @@ Public Class MSSQLServer Using oConnection = GetSQLConnection() Using oSQLCOmmand = oConnection.CreateCommand() - oSQLCOmmand.CommandText = executeStatement + oSQLCOmmand.CommandText = SQLCommand oSQLCOmmand.CommandTimeout = Timeout oSQLCOmmand.ExecuteNonQuery() Return True @@ -94,7 +99,7 @@ Public Class MSSQLServer End Using Catch ex As Exception _Logger.Error(ex) - _Logger.Debug("executeStatement: " & executeStatement) + _Logger.Debug("executeStatement: " & SQLCommand) Return False End Try End Function @@ -107,6 +112,11 @@ Public Class MSSQLServer ''' Returns true if properly executed, else false ''' Public Function NewExecuteScalar(ScalarSQL As String, Optional Timeout As Integer = 120) As Object + _Logger.Warn("NewExecuteScalar is deprecated. Use GetScalarValue instead.") + Return GetScalarValue(ScalarSQL, Timeout) + End Function + + Public Function GetScalarValue(SQLQuery As String, Optional Timeout As Integer = 120) As Object Try If TestCanConnect() = False Then Return Nothing @@ -114,7 +124,7 @@ Public Class MSSQLServer Using oConnection As SqlConnection = GetSQLConnection() Using oSQLCOmmand = oConnection.CreateCommand() - oSQLCOmmand.CommandText = ScalarSQL + oSQLCOmmand.CommandText = SQLQuery oSQLCOmmand.CommandTimeout = Timeout Dim oResult As Object = oSQLCOmmand.ExecuteScalar() Return oResult @@ -122,7 +132,6 @@ Public Class MSSQLServer End Using Catch ex As Exception _Logger.Error(ex) - _Logger.Debug("executeStatement: " & ScalarSQL) Return Nothing End Try End Function diff --git a/Modules.Database/ODBC.vb b/Modules.Database/ODBC.vb new file mode 100644 index 00000000..2d95c07d --- /dev/null +++ b/Modules.Database/ODBC.vb @@ -0,0 +1,179 @@ +Imports System.Data.Odbc +Imports DigitalData.Modules.Logging + +Public Class ODBC + Private _Logger As Logger + Private _LogConfig As LogConfig + + Private _connectionDatasource As String + Private _connectionUsername As String + Private _connectionPassword As String + Private _connectionString As String + + Public Sub New(LogConfig As LogConfig, Datasource As String, User As String, Password As String) + Try + _LogConfig = LogConfig + _Logger = _LogConfig.GetLogger() + + _connectionDatasource = Datasource + _connectionPassword = Password + _connectionUsername = User + _connectionString = GetConnectionString(Datasource, User, Password) + + _Logger.Debug("Connecting to database..") + + ' Test the connection + Dim oConnection = GetConnection() + + If oConnection Is Nothing Then + Throw New Exceptions.DatabaseException() + End If + + ' If initial connection was successfully, close it + oConnection.Close() + + _Logger.Debug("Connection sucessfully established!") + Catch ex As Exception + _Logger.Error(ex) + End Try + End Sub + + Public Function GetConnection() As OdbcConnection + Try + Dim oConnection As New OdbcConnection(_connectionString) + oConnection.Open() + + Return oConnection + Catch ex As Exception + _Logger.Error(ex) + Return Nothing + End Try + End Function + + Public Function GetConnectionString(Datasource As String, User As String, Password As String) As Object + Return $"DSN={Datasource};UID={User};PWD={Password}" + End Function + + ''' + ''' Executes a non-query command. + ''' + ''' The command to execute + ''' The Firebird connection to use + ''' True, if command was executed sucessfully. Otherwise false. + Public Function ExecuteNonQueryWithConnection(SqlQuery As String, Connection As OdbcConnection) As Object + _Logger.Debug("Fetching Non-Query: {0}", SqlQuery) + + Dim oResult As Object + + If Connection Is Nothing Then + _Logger.Warn("Connection is nothing!") + Return Nothing + End If + + Try + Dim oCommand As New OdbcCommand(SqlQuery, Connection) + oResult = oCommand.ExecuteNonQuery() + Catch ex As Exception + _Logger.Error(ex, $"Error in ExecuteNonQueryWithConnection while executing command: '{SqlQuery}'") + Throw ex + End Try + + Return oResult + End Function + + ''' + ''' Executes a non-query command. + ''' + ''' The command to execute + ''' True, if command was executed sucessfully. Otherwise false. + Public Function ExecuteNonQuery(SqlCommand As String) As Boolean + Dim oConnection As OdbcConnection = GetConnection() + Dim oScalarValue As Object = ExecuteNonQueryWithConnection(SqlCommand, oConnection) + oConnection.Close() + + Return oScalarValue + End Function + + ''' + ''' Executes a sql query resulting in a scalar value. + ''' + ''' The query to execute + ''' The Firebird connection to use + ''' The scalar value if the command was executed successfully. Nothing otherwise. + Public Function GetScalarValueWithConnection(SqlQuery As String, Connection As OdbcConnection) As Object + _Logger.Debug("Fetching Datatable: {0}", SqlQuery) + + Dim oResult As Object + + If Connection Is Nothing Then + _Logger.Warn("Connection is nothing!") + Return Nothing + End If + + Try + Dim oCommand As New OdbcCommand(SqlQuery, Connection) + oResult = oCommand.ExecuteScalar() + Catch ex As Exception + _Logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'") + Throw ex + End Try + + Return oResult + End Function + + ''' + ''' Executes a sql query resulting in a table of values. + ''' + ''' The query to execute + ''' A datatable containing the results if the command was executed successfully. Nothing otherwise. + Public Function GetScalarValue(SqlQuery As String) As Object + Dim oConnection As OdbcConnection = GetConnection() + Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection) + oConnection.Close() + + Return oDatatable + End Function + + ''' + ''' Executes a sql query resulting in a table of values. + ''' + ''' The query to execute + ''' The Firebird connection to use + ''' A datatable containing the results if the command was executed successfully. Nothing otherwise. + Public Function GetDatatableWithConnection(SqlQuery As String, Connection As OdbcConnection) As DataTable + _Logger.Debug("Fetching Datatable: {0}", SqlQuery) + + Dim oDatatable As New DataTable() With { + .TableName = "DDRESULT" + } + + If Connection Is Nothing Then + _Logger.Warn("Connection is nothing!") + Return Nothing + End If + + Try + + Dim oAdapter As New OdbcDataAdapter(SqlQuery, Connection) + oAdapter.Fill(oDatatable) + Catch ex As Exception + _Logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'") + Throw ex + End Try + + Return oDatatable + End Function + + ''' + ''' Executes a sql query resulting in a table of values. + ''' + ''' The query to execute + ''' A datatable containing the results if the command was executed successfully. Nothing otherwise. + Public Function GetDatatable(SqlQuery As String) As DataTable + Dim oConnection As OdbcConnection = GetConnection() + Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection) + oConnection.Close() + + Return oDatatable + End Function +End Class