168 lines
5.9 KiB
VB.net
168 lines
5.9 KiB
VB.net
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
|
|
|
|
''' <summary>
|
|
''' Builds a connectionstring from the provided arguments.
|
|
''' </summary>
|
|
''' <param name="dataSource">The database server where to connect to</param>
|
|
''' <param name="database">The datasource, eg. the path of the FDB-file</param>
|
|
''' <param name="user">The user used to connect to the database</param>
|
|
''' <param name="password">The password of the connecting user</param>
|
|
''' <returns>A connectionstring</returns>
|
|
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
|
|
|
|
''' <summary>
|
|
''' Executes a non-query command.
|
|
''' </summary>
|
|
''' <param name="sqlCommand">The command to execute</param>
|
|
''' <returns>True, if command was executed sucessfully. Otherwise false.</returns>
|
|
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
|
|
|
|
''' <summary>
|
|
''' Executes a sql query resulting in a scalar value.
|
|
''' </summary>
|
|
''' <param name="sqlQuery">The query to execute</param>
|
|
''' <returns>The scalar value if the command was executed successfully. Nothing otherwise.</returns>
|
|
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
|
|
|
|
''' <summary>
|
|
''' Executes a sql query resulting in a table of values.
|
|
''' </summary>
|
|
''' <param name="sqlQuery">The query to execute</param>
|
|
''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns>
|
|
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
|