Imports FirebirdSql.Data.FirebirdClient Public Class Firebird Private Shared Logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger Public DBInitialized As Boolean = False Private _connectionEstablished As Boolean = False Private _connectionFailed As Boolean = False Private ReadOnly dataSource As String Private ReadOnly database As String Private ReadOnly user As String Private ReadOnly password As String Public CurrentFBDConnectionString As String = "" Public ReadOnly Property ConnectionString As String Public ReadOnly Property ConnectionEstablished As Boolean Get Return _connectionEstablished End Get End Property Public ReadOnly Property ConnectionFailed As Boolean Get Return _connectionFailed End Get End Property Public Sub New(dataSource As String, database As String, user As String, password As String) ConnectionString = BuildConnectionString(dataSource, database, user, password) ' Save connection credentials dataSource = dataSource database = database user = user password = password ' Test the connection first Dim conn = Connect(ConnectionString) ' If initial connection was successfully, close it conn?.Close() End Sub Private Function Connect(ConnectionString As String) As FbConnection Try Dim conn = New FbConnection(ConnectionString) conn.Open() _connectionEstablished = True _connectionFailed = False Logger.Debug("Connection established!") Logger.Debug($"User: {user}") Logger.Debug($"DatabaseLocation: {database}") Logger.Debug($"DatabaseServer: {dataSource}") Return conn 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 BuildConnectionString(dataSource As String, database As String, user As String, password As String) As String Dim connectionStringBuilder = New FbConnectionStringBuilder With { .DataSource = dataSource, .Database = database, .UserID = user, .Password = password } Return connectionStringBuilder.ConnectionString End Function ''' ''' Executes a non-query command. ''' ''' The command to execute ''' True, if command was executed sucessfully. Otherwise false. Public Function NewExecuteNonQuery(sqlCommand As String) As Boolean Try Dim conn As FbConnection = Connect(ConnectionString) If conn Is Nothing Then Return False End If Dim transaction As FbTransaction = conn.BeginTransaction() Dim command As New FbCommand With { .CommandText = sqlCommand, .Connection = conn, .Transaction = transaction } command.ExecuteNonQuery() transaction.Commit() conn.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 GetExecuteScalar(sqlQuery As String) As Object Try Dim conn As FbConnection = Connect(ConnectionString) If conn Is Nothing Then Return Nothing End If Dim transaction As FbTransaction = conn.BeginTransaction() Dim command As New FbCommand With { .CommandText = sqlQuery, .Connection = conn, .Transaction = transaction } Dim result As Object = command.ExecuteScalar() transaction.Commit() conn.Close() Return result 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 conn As FbConnection = Connect(ConnectionString) If conn Is Nothing Then Return Nothing End If Dim command As New FbCommand With { .CommandText = sqlQuery, .Connection = conn } Dim adapter As New FbDataAdapter(command) Dim dt As New DataTable() adapter.Fill(dt) conn.Close() Return dt Catch ex As Exception Logger.Error(ex, $"Error in ReturnDatatable while executing command: '{sqlQuery}'") Return Nothing End Try End Function End Class