I have this function I am using with in my Windows Application and it makes my code very easy to read and use and I would like to port it to FreeBASIC
Code: Select all
Dim aQuery = QueryDatabase("database", SQLCommand)
If aQuery.HasRows = True Then
'aQuery.Read()
CashierNameSystemWide = aQuery.Item("username")
CashierNumberSystemWide = aQuery.Item("userid")
frmcashierSignOn.labCashierName.Text = CashierNameSystemWide
aQuery.Finished()
Else
frmcashierSignOn.Enabled = False
aQuery.Finished()
With FrmErrorMessage
.labCallingForm.Text = "CashierID"
.labErrorMessage.Text = vbNullString
.labErrorMessage.Text += "the Cashier ID you just entered can not be found in the system." & vbNewLine
.labErrorMessage.Text += vbNewLine
.labErrorMessage.Text += "Please check and try again" & vbNewLine
.MdiParent = frmBackground
.Show()
.Focus()
End With
End If
Code: Select all
Public Function QueryDatabase(ByVal from As String, ByVal aQuery As String) As DatabaseQuery
Dim aConnection = ConnectToDatabaseServer(from), aCommand = New FbCommand(aQuery, aConnection)
Dim aReader = aCommand.ExecuteReader()
Return (New DatabaseQuery(aConnection, aCommand, aReader))
End Function
Code: Select all
Option Infer On
Imports FirebirdSql.Data.FirebirdClient
Imports System.IO
' This Module Handles all the Database Functions
Module DatabaseFunctions
Dim Exception As String
Public Class DatabaseQuery
Public Sub New(ByVal aConnection As FbConnection, ByVal aCommand As FbCommand, ByVal aDataReader As FbDataReader)
_connection = aConnection
_command = aCommand
_reader = aDataReader
End Sub
Protected Overrides Sub Finalize()
Finished()
End Sub
''' <summary>
''' Retrieves an item by key.
''' </summary>
''' <param name="key">The key of the item to retrieve.</param>
Default Public ReadOnly Property Item(ByVal key As String) As Object
Get
Return (_reader.Item(key))
End Get
End Property
''' <summary>
''' Retrieves an item by index.
''' </summary>
''' <param name="index">The index of the item to retrieve.</param>
Default Public ReadOnly Property Item(ByVal index As Integer) As Object
Get
Return (_reader.Item(index))
End Get
End Property
''' <summary>
''' Gets whether or not the query has any rows.
''' </summary>
Public ReadOnly Property HasRows() As Boolean
Get
Return (_reader.HasRows)
End Get
End Property
Public Function Read() As Boolean
Return (_reader.Read())
End Function
''' <summary>
''' Finishes the query and releases all resources.
''' </summary>
Public Sub Finished()
If (_reader IsNot Nothing) Then
If _reader.IsClosed = False Then
'_reader.Close()
_reader = Nothing
End If
End If
If (_connection IsNot Nothing) Then
If (_connection.State = ConnectionState.Open) Then
_connection.Close()
End If
End If
End Sub
Private _connection As FbConnection, _command As FbCommand, _reader As FbDataReader
End Class
Private Function ConnectToDatabaseServer(ByVal ConnectionType As String) As FbConnection
Dim aConnection As FbConnection
Select Case (ConnectionType.ToLower())
Case "database"
dbConn = New FbConnection(FBLocalDatabaseLocation)
dbConn.Open()
aConnection = dbConn
Case "thread"
ThreaddbConn = New FbConnection(FBLocalDatabaseLocation)
ThreaddbConn.Open()
aConnection = ThreaddbConn
Case "vat"
VATdbConn = New FbConnection(FBLocalDatabaseLocation)
VATdbConn.Open()
aConnection = VATdbConn
Case "msupdate"
MSUpdatedbConn = New FbConnection(FBLocalDatabaseLocation)
MSUpdatedbConn.Open()
aConnection = MSUpdatedbConn
Case "sendtodatabase"
SendToDBConn = New FbConnection(FBLocalDatabaseLocation)
SendToDBConn.Open()
aConnection = SendToDBConn
Case Else
Throw New ArgumentException("The database you requested is not valid.")
End Select
Return (aConnection)
End Function
Public Function QueryDatabase(ByVal from As String, ByVal aQuery As String) As DatabaseQuery
Dim aConnection = ConnectToDatabaseServer(from), aCommand = New FbCommand(aQuery, aConnection)
Dim aReader = aCommand.ExecuteReader()
Return (New DatabaseQuery(aConnection, aCommand, aReader))
End Function
I know there will be a lot of converting to do but if someone could point me in the correct direction I would be most grateful (I want to use this code as it has powered my Windows app for over 5 years and the database access is stable and smooth EVERY TIME i need to use it)