From 5748d37227044564762f61fb42d75fc1ff4e7fe7 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 9 Nov 2018 11:18:00 +0100 Subject: [PATCH] jj: Database Version 0.0.3 - finally --- Modules.Database/Firebird.vb | 119 +++++++++++++------- Modules.Database/My Project/AssemblyInfo.vb | 2 +- 2 files changed, 80 insertions(+), 41 deletions(-) diff --git a/Modules.Database/Firebird.vb b/Modules.Database/Firebird.vb index c2a9b95c..59ef39de 100644 --- a/Modules.Database/Firebird.vb +++ b/Modules.Database/Firebird.vb @@ -5,9 +5,9 @@ Imports DigitalData.Modules.Logging ''' ''' MODULE: Firebird ''' -''' VERSION: 0.0.0.2 +''' VERSION: 0.0.0.3 ''' -''' DATE: 03.09.2018 +''' DATE: 08.11.2018 ''' ''' DESCRIPTION: ''' @@ -17,15 +17,15 @@ Imports DigitalData.Modules.Logging ''' ''' FirebirdSql.Data.FirebirdClient, >= 6.0.0 ''' -''' PARAMETERS: LogFactory, NLog.LogFactory +''' PARAMETERS: LogConfig, DigitalData.Modules.Logging.LogConfig ''' The LogFactory containing the current log config. Used to instanciate the class logger for this and any dependent class ''' ''' DataSource, String -''' The location of the Database in the format `127.0.0.1:E:\Path\To\Database.FDB` -''' -''' Database, String ''' The server where the database lives, for example 127.0.0.1 or dd-vmx09-vm03 ''' +''' Database, String +''' The location of the Database in the format `127.0.0.1:E:\Path\To\Database.FDB` +''' ''' User, String ''' The user name to connect as ''' @@ -52,23 +52,26 @@ Public Class Firebird Private _connectionDatabase As String Private _connectionUsername As String Private _connectionPassword As String + Private _connectionString As String - Public ReadOnly Property ConnectionEstablished As Boolean - Public ReadOnly Property ConnectionFailed As Boolean Public ReadOnly Property ConnectionString As String + Get + Return _connectionString + End Get + End Property + Public ReadOnly Property DatabaseName As String Get Dim oRegex As New Regex("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:") Dim oPath As String = oRegex.Replace(_connectionDatabase, "") - Dim fi As New IO.FileInfo(oPath) - Return fi.Name + Dim oFileInfo As New IO.FileInfo(oPath) + Return oFileInfo.Name End Get End Property ''' ''' ''' - ''' ''' ''' ''' @@ -84,7 +87,7 @@ Public Class Firebird _connectionDatabase = Database _connectionUsername = User _connectionPassword = Password - _ConnectionString = oConnectionString + _connectionString = oConnectionString ' Test the connection Dim oConnection = GetConnection() @@ -98,15 +101,11 @@ Public Class Firebird Public Function GetConnection() As FbConnection Try - Dim oConnection = New FbConnection(_ConnectionString) + Dim oConnection = New FbConnection(_connectionString) oConnection.Open() - _ConnectionEstablished = True - _ConnectionFailed = False Return oConnection Catch ex As Exception - _ConnectionFailed = True - _ConnectionEstablished = False _logger.Error(ex) Return Nothing @@ -134,16 +133,18 @@ Public Class Firebird ''' Executes a non-query command. ''' ''' The command to execute + ''' The Firebird connection to use ''' True, if command was executed sucessfully. Otherwise false. - Public Function ExecuteNonQuery(SqlCommand As String) As Boolean - Try - Dim oConnection As FbConnection = GetConnection() + Public Function ExecuteNonQueryWithConnection(SqlCommand As String, Connection As FbConnection) As Boolean + Dim oConnection As FbConnection = GetConnection() + Dim oTransaction As FbTransaction - If oConnection Is Nothing Then - Return False - End If + If oConnection Is Nothing Then + Return False + End If - Dim oTransaction As FbTransaction = oConnection.BeginTransaction() + Try + oTransaction = oConnection.BeginTransaction() Dim oCommand As New FbCommand With { .CommandText = SqlCommand, .Connection = oConnection, @@ -160,29 +161,41 @@ Public Class Firebird End Try 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 FbConnection = 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 GetScalarValue(SqlQuery As String) As Object + Public Function GetScalarValueWithConnection(SqlQuery As String, Connection As FbConnection) As Object Try - Dim oConnection As FbConnection = GetConnection() - - If oConnection Is Nothing Then + If Connection Is Nothing Then Return Nothing End If - Dim oTransaction As FbTransaction = oConnection.BeginTransaction() + Dim oTransaction As FbTransaction = Connection.BeginTransaction() Dim oCommand As New FbCommand With { .CommandText = SqlQuery, - .Connection = oConnection, + .Connection = Connection, .Transaction = oTransaction } Dim oResult As Object = oCommand.ExecuteScalar() oTransaction.Commit() - oConnection.Close() Return oResult Catch ex As Exception @@ -191,33 +204,59 @@ Public Class Firebird End Try End Function + ''' + ''' Executes a sql query resulting in a scalar value. + ''' + ''' The query to execute + ''' The scalar value if the command was executed successfully. Nothing otherwise. + Public Function GetScalarValue(SqlQuery As String) As Object + Dim oConnection As FbConnection = GetConnection() + Dim oScalarValue As Object = GetScalarValueWithConnection(SqlQuery, oConnection) + + oConnection.Close() + + Return oScalarValue + 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 GetDatatable(SqlQuery As String) As DataTable + Public Function GetDatatableWithConnection(SqlQuery As String, Connection As FbConnection) As DataTable Try - Dim oConnection As FbConnection = GetConnection() - - If oConnection Is Nothing Then + If Connection Is Nothing Then Return Nothing End If Dim oCommand As New FbCommand With { .CommandText = SqlQuery, - .Connection = oConnection + .Connection = Connection } Dim oAdapter As New FbDataAdapter(oCommand) - Dim oDatatable As New DataTable() + Dim oDatatable As New DataTable() With {.TableName = "DDRESULT"} oAdapter.Fill(oDatatable) - oConnection.Close() Return oDatatable Catch ex As Exception - _logger.Error(ex, $"Error in ReturnDatatable while executing command: '{SqlQuery}'") - Return Nothing + _logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'") + Throw ex End Try 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 FbConnection = GetConnection() + Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection) + + oConnection.Close() + + Return oDatatable + End Function End Class diff --git a/Modules.Database/My Project/AssemblyInfo.vb b/Modules.Database/My Project/AssemblyInfo.vb index a9b1823b..2c942633 100644 --- a/Modules.Database/My Project/AssemblyInfo.vb +++ b/Modules.Database/My Project/AssemblyInfo.vb @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices ' übernehmen, indem Sie "*" eingeben: ' - +