From c27cf5e8a0e9859338d83b9bf473a6f3067fd863 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 9 Nov 2018 10:49:59 +0100 Subject: [PATCH] jj: Database Version 0.0.3 --- Modules.Database/Firebird.vb | 119 +++++++------------- Modules.Database/My Project/AssemblyInfo.vb | 2 +- 2 files changed, 41 insertions(+), 80 deletions(-) diff --git a/Modules.Database/Firebird.vb b/Modules.Database/Firebird.vb index 59ef39de..c2a9b95c 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.3 +''' VERSION: 0.0.0.2 ''' -''' DATE: 08.11.2018 +''' DATE: 03.09.2018 ''' ''' DESCRIPTION: ''' @@ -17,15 +17,15 @@ Imports DigitalData.Modules.Logging ''' ''' FirebirdSql.Data.FirebirdClient, >= 6.0.0 ''' -''' PARAMETERS: LogConfig, DigitalData.Modules.Logging.LogConfig +''' PARAMETERS: LogFactory, NLog.LogFactory ''' The LogFactory containing the current log config. Used to instanciate the class logger for this and any dependent class ''' ''' DataSource, 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` ''' +''' Database, String +''' The server where the database lives, for example 127.0.0.1 or dd-vmx09-vm03 +''' ''' User, String ''' The user name to connect as ''' @@ -52,26 +52,23 @@ 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 oFileInfo As New IO.FileInfo(oPath) - Return oFileInfo.Name + Dim fi As New IO.FileInfo(oPath) + Return fi.Name End Get End Property ''' ''' ''' + ''' ''' ''' ''' @@ -87,7 +84,7 @@ Public Class Firebird _connectionDatabase = Database _connectionUsername = User _connectionPassword = Password - _connectionString = oConnectionString + _ConnectionString = oConnectionString ' Test the connection Dim oConnection = GetConnection() @@ -101,11 +98,15 @@ 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 @@ -133,18 +134,16 @@ 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 ExecuteNonQueryWithConnection(SqlCommand As String, Connection As FbConnection) As Boolean - Dim oConnection As FbConnection = GetConnection() - Dim oTransaction As FbTransaction + Public Function ExecuteNonQuery(SqlCommand As String) As Boolean + Try + Dim oConnection As FbConnection = GetConnection() - If oConnection Is Nothing Then - Return False - End If + If oConnection Is Nothing Then + Return False + End If - Try - oTransaction = oConnection.BeginTransaction() + Dim oTransaction As FbTransaction = oConnection.BeginTransaction() Dim oCommand As New FbCommand With { .CommandText = SqlCommand, .Connection = oConnection, @@ -161,41 +160,29 @@ 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 GetScalarValueWithConnection(SqlQuery As String, Connection As FbConnection) As Object + Public Function GetScalarValue(SqlQuery As String) As Object Try - If Connection Is Nothing Then + Dim oConnection As FbConnection = GetConnection() + + If oConnection Is Nothing Then Return Nothing End If - Dim oTransaction As FbTransaction = Connection.BeginTransaction() + Dim oTransaction As FbTransaction = oConnection.BeginTransaction() Dim oCommand As New FbCommand With { .CommandText = SqlQuery, - .Connection = Connection, + .Connection = oConnection, .Transaction = oTransaction } Dim oResult As Object = oCommand.ExecuteScalar() oTransaction.Commit() + oConnection.Close() Return oResult Catch ex As Exception @@ -204,59 +191,33 @@ 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 GetDatatableWithConnection(SqlQuery As String, Connection As FbConnection) As DataTable + Public Function GetDatatable(SqlQuery As String) As DataTable Try - If Connection Is Nothing Then + Dim oConnection As FbConnection = GetConnection() + + If oConnection Is Nothing Then Return Nothing End If Dim oCommand As New FbCommand With { .CommandText = SqlQuery, - .Connection = Connection + .Connection = oConnection } Dim oAdapter As New FbDataAdapter(oCommand) - Dim oDatatable As New DataTable() With {.TableName = "DDRESULT"} + Dim oDatatable As New DataTable() oAdapter.Fill(oDatatable) + oConnection.Close() Return oDatatable Catch ex As Exception - _logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'") - Throw ex + _logger.Error(ex, $"Error in ReturnDatatable while executing command: '{SqlQuery}'") + Return Nothing 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 2c942633..a9b1823b 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: ' - +