Imports NLog Imports FirebirdSql.Data.FirebirdClient Imports System.Text.RegularExpressions ''' ''' MODULE: Firebird ''' ''' VERSION: 0.0.0.1 ''' ''' DATE: 03.09.2018 ''' ''' DESCRIPTION: ''' ''' DEPENDENCIES: NLog, >= 4.5.8 ''' ''' EntityFramework.Firebird, >= 6.1.0 ''' ''' FirebirdSql.Data.FirebirdClient, >= 6.0.0 ''' ''' 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 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 ''' ''' Password, String ''' The user's password ''' ''' PROPERTIES: ConnectionEstablished, Boolean ''' If the last opened connection was successful ''' ''' ConnectionFailed, Boolean ''' If the last opened connection failed ''' ''' ConnectionString, String ''' The used connectionstring ''' ''' EXAMPLES: ''' ''' REMARKS: If the connection fails due to "wrong username or password", the cause might be that the server harddrive is full.. ''' Public Class Firebird Private _logger As Logger Private _logFactory As LogFactory Private _connectionServer As String Private _connectionDatabase As String Private _connectionUsername As String Private _connectionPassword As String Public ReadOnly Property ConnectionEstablished As Boolean Public ReadOnly Property ConnectionFailed As Boolean Public ReadOnly Property ConnectionString As String 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 End Get End Property ''' ''' ''' ''' ''' ''' ''' ''' ''' Public Sub New(LogFactory As LogFactory, Datasource As String, Database As String, User As String, Password As String) _logger = LogFactory.GetCurrentClassLogger() _logFactory = LogFactory Dim oConnectionString = GetConnectionString(Datasource, Database, User, Password) _connectionServer = Datasource _connectionDatabase = Database _connectionUsername = User _connectionPassword = Password _ConnectionString = oConnectionString ' Test the connection Dim oConnection = GetConnection() ' If initial connection was successfully, close it oConnection?.Close() If oConnection Is Nothing Then Throw New Exceptions.DatabaseException() End If End Sub Public Function GetConnection() As FbConnection Try 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 End Try End Function ''' ''' Builds a connectionstring from the provided arguments. ''' ''' The database server where to connect to ''' The datasource, eg. the path of the FDB-file ''' The user used to connect to the database ''' The password of the connecting user ''' A connectionstring Private Function GetConnectionString(DataSource As String, Database As String, User As String, Password As String) As String Return New FbConnectionStringBuilder With { .DataSource = DataSource, .Database = Database, .UserID = User, .Password = Password }.ToString() 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 Try Dim oConnection As FbConnection = GetConnection() If oConnection Is Nothing Then Return False End If Dim oTransaction As FbTransaction = oConnection.BeginTransaction() Dim oCommand As New FbCommand With { .CommandText = SqlCommand, .Connection = oConnection, .Transaction = oTransaction } oCommand.ExecuteNonQuery() oTransaction.Commit() oConnection.Close() Return True Catch ex As Exception _logger.Error(ex, $"Error in ExecuteNonQuery while executing command: '{SqlCommand}'") Return False 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 Try Dim oConnection As FbConnection = GetConnection() If oConnection Is Nothing Then Return Nothing End If Dim oTransaction As FbTransaction = oConnection.BeginTransaction() Dim oCommand As New FbCommand With { .CommandText = SqlQuery, .Connection = oConnection, .Transaction = oTransaction } Dim oResult As Object = oCommand.ExecuteScalar() oTransaction.Commit() oConnection.Close() Return oResult Catch ex As Exception _logger.Error(ex, $"Error in ReturnScalar 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 Try Dim oConnection As FbConnection = GetConnection() If oConnection Is Nothing Then Return Nothing End If Dim oCommand As New FbCommand With { .CommandText = SqlQuery, .Connection = oConnection } Dim oAdapter As New FbDataAdapter(oCommand) Dim oDatatable As New DataTable() oAdapter.Fill(oDatatable) oConnection.Close() Return oDatatable Catch ex As Exception _logger.Error(ex, $"Error in ReturnDatatable while executing command: '{SqlQuery}'") Return Nothing End Try End Function End Class