Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

VB.NET Class Library: DBIO Access

0.00/5 (No votes)
11 Jun 2010 1  
A plug-in database I/O layer for Access databases with automatic SQL statements

Introduction

The DBIO Access class library (tgs_dbio_access.dll) encapsulates one class named C_DBIO_Access. This class is a plug-in I/O layer for Access databases which includes the automatic generation of SQL statements.

Using The Class Library In A Visual Studio Project

  1. In Visual Studio, add the tgs_dbio_access.dll class library to a project's References.
  2. Add Imports tgs_dbio_access at the top of each module in which the class is consumed.

Constructors

The C_DBIO_Access class exposes three constructor options.

Constructor Option Signature Description
No parameters Empty Instantiate a skeleton object with all properties set to their default values.
    Dim dba As New C_DBIO_Access
Database path String The database path is validated and, if valid, a database connection string is built without a password.
    Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
Database path and password String, String The database password property is set. The database path is validated and, if valid, a database connection string is built including the password.
    Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb", _
                                 "password")

Methods

The C_DBIO_Access class exposes five methods. They are listed here in alphabetical order:

Method Signature Description
DeleteRow DataTable, String Builds an SQL DELETE command to remove a row from an Access table. The String parameter is the WHERE expression that identifies the row (or rows) to be deleted. Updates the database and returns True if the delete is successful. False is returned should the delete fail. In the latter case, an error message is displayed that includes the exception Message property along with the complete SQL DELETE statement.
    Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
    Dim dtb As New DataTable
    With dba
        '** Generate a SELECT * statement 
        '** for the database table
        .DatabaseTableName(True) = "Customers"
        '** Extract rows from the database table
        If .PopulateDataTable() Then
            '** Delete the customer from the database
            '** WHERE the CustID is 123
            dtb = .FilledDataTable
            If Not .DeleteRow(dtb, "CustID = '123'") Then
                '** Code to execute when delete fails
            End If
        Else
            '** Code to execute when table 
            '** population fails
        End If
    End With
InsertRow DataTable,
(optional)
String Array,
(optional)
Integer Array
Builds an SQL INSERT command to add a row to an Access table. The row being added is assumed to be the highest indexed row in the DataTable. Updates the database and returns True if the insertion is successful. False is returned should the insertion fail. In the latter case, an error message is displayed that includes the exception Message property along with the complete SQL INSERT statement.

The optional String Array contains the names of datatable columns that are not to be included in the INSERT statement. The names are case sensitive. The names in the array may be in any order.

The optional Integer Array contains the indexes (zero based) of datatable columns that are not to be included in the INSERT statement. The indexes in the array may be in any order.

One or both of the arrays may be passed. The string array is checked first. If a column name is not in the string array, the integer array is checked.

    '** Instantiate a datatable for the database table
    Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
    Dim dtb As New DataTable
    With dba
       '** Limit the number of datatable rows returned
       .SQLTop = 1
       '** Generate a SELECT * statement 
       '** for the database table
       .DatabaseTableName(True) = "Customers"
       '** Extract a row from the database table
        If .PopulateDataTable Then
            dtb = .FilledDataTable
        Else
            '** Code executed when population fails
        End If
    End With
    '** Add and populate a new row in the datatable
    Dim dr As DataRow = dtb.NewRow
    dr("CustID") = "456"
    dr("CustName") = "Acme Products"
    dt.Rows.Add(dr)
    dr = Nothing
    '** Build an array of table columns not being 
    '** populated in the new row
    Dim skip(1) As String
    skip(0) = "CustAddress"
    skip(1) = "CustPhone"
    '** Insert the new row into the database without 
    '** an address or phone number
    With dba
        If Not .InsertRow(dtb, skip) Then
            '** Code to execute when insert fails
        End If
    End With
PopulateDataTable (Optional) Boolean

Populates the FilledDataTable property using the SQLString property (either set manually or automatically generated). If any part of the DataTable fill process fails, the FilledDataTable property is not changed. True is returned when the DataTable fill is successful; False if the process fails.

The optional Boolean parameter for DisplayError controls whether an error message is displayed when the fill process fails. The default is True. When an error message is displayed it includes the exception Message property along with the complete SQL SELECT statement. When the DisplayError parameter is False, the exception Message property is exposed via the ErrorMessage property.

The name of the filled DataTable is set to the DatabaseTableName property if that property has been set. Otherwise, the name of the Access table, from which the data was extracted, is used after being parsed from the SQLString property text.

UpdateRow DataTable, Integer, (Optional) String,
(optional)
String Array,
(optional)
Integer Array
Builds an SQL UPDATE command to update a row in an Access table from the corresponding changed row in the DataTable. The Integer parameter is the index of the DataTable row that was changed.

The optional String parameter is the WHERE expression that identifies one or more Access table rows to be updated. Not supplying a WHERE expression causes all rows in the table to be updated.

The optional String Array contains the names of datatable columns that are not to be included in the UPDATE statement. The names are case sensitive. The names in the array may be in any order.

The optional Integer Array contains the indexes (zero based) of datatable columns that are not to be included in the UPDATE statement. The indexes in the array may be in any order.

Updates the database and returns True if the update is successful. False is returned should the update fail. In the latter case, an error message is displayed that includes the exception Message property along with the complete SQL UPDATE statement.

    '** Instantiate a datatable for the database table
    Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
    Dim dtb As New DataTable
    With dba
        '** Retrieve customer ID 456
        .SQLWhere = "CustID = '456'"
        '** Generate a SELECT * statement 
        '** for the database table
        .DatabaseTableName(True) = "Customers"
        '** Extract row from the database table
        If .PopulateDataTable Then
            dtb = .FilledDataTable
        Else
            '** Code executed when population fails
        End If
    End With
    '** Update the row in the datatable
    With dtb.Rows(0)
        .Item("CustAddress") = "18 Hill Drive, Anywhere, USA"
        .Item("CustPhone") = "111-222-5555"
    End With
    '** Build an array of table columns not being 
    '** updated
    Dim skip(1) As String
    skip(0) = "CustID"
    skip(1) = "CustName"
    '** Update the row in the database
    If Not dba.UpdateRow(dtb, _
                         0, _
                         "CustID = '456', _
                         skip) Then
        '** Code to execute when update fails
    End If
UseCommandBuilder None Updates an Access table with transactions generated by the CommandBuilder object. Transactions are based on the FilledDataTable and SQLString properties. The SQLString property must contain exactly the same SQL query that was originally used to populate the DataTable. Returns the total number of Access table rows that were added, updated, and deleted. If the database update fails, -1 is returned, and an error message is displayed that includes the exception Message property, the complete SQLString property, and the complete database connection string.
    '** Instantiate a datatable for the database table
    Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
    Dim dtb As New DataTable
    With dba
        '** Generate a SELECT * statement 
        '** for the database table
        .DatabaseTableName(True) = "Customers"
        '** Extract rows from the database table
        If .PopulateDataTable Then
            dtb = .FilledDataTable
        Else
            '** Code executed when population fails
        End If
    End With
    '** Add, update and/or delete one or more rows 
    '** in the datatable
    With dtb.Rows(IndexOfRowBeingChanged)
        .Item("CustAddress") = "18 Hill Drive, Anywhere, USA"
        .Item("CustPhone") = "111-222-5555"
    End With
    '** Update the added, changed and deleted row(s) 
    '** in the database (It is critical that the SQLString
    '** property used above to generate the datatable 
    '** is not changed between populating the datatable 
    '** and invoking the UseCommandBuilder method)
    With dba
        .FilledDataTable = dtb
        If .UseCommandBuilder < 0 Then
            '** Code to execute when update fails
        End If
    End With

Properties

The C_DBIO_Access class exposes fifteen properties. They are listed here in alphabetical order:

Property Variable Type Set Value (=) or (Parameters) Description
ConnectionReady Boolean N/A Get the state of the database connection string.
DatabasePassword N/A = String Set the password parameter for the database connection string. Setting the property to Nothing or an empty string clears the property. If the DatabasePath property is set, the database connection string is re-built to update or remove the password parameter.
DatabasePath String = String Get/set the fully qualified path to the Access database. If the database path validates, the connection string is rebuilt and the ConnectionReady property is set to True. If the path to the database is not valid, the database connection string is cleared and the ConnectionReady property is set to False.
DatabaseTableName String (Boolean) defaults to False Get/set the name of the table in the Access database. If the passed table name is enclosed in brackets, the brackets are removed (the table name is enclosed in brackets whenever the name is used in an SQL statement). When a True parameter is also passed, the SQLString property is populated with an SQL SELECT * command for the table.
ErrorMessage String N/A Get the exception error message generated when the PopulateDataTable method failed with the DisplayError parameter set to False.
FilledDataTable DataTable = DataTable Get/set the DataTable.
SQLDistinct Boolean = Boolean Get/set the flag that specifies if the DISTINCT keyword is to be included when the SQL SELECT statement is built.
SQLOrderBy String = String Get/set the ORDER BY expression to be included when the SQL SELECT statement is built. When this property is not set, the ORDER BY clause is not included in the SELECT command.
    '** Sort the .FilledDataTable on the CustID column
    dba.SQLOrderBy = "CustID"
SQLOrderByAscending Boolean = Boolean Get/set the flag that specifies if the DataTable is to be sorted in ascending or descending sequence. When True, ASC is appended to the ORDER BY clause. DESC is appended when this property is False.
SQLSelectList String = String Get/set the property holding the list of fields to include in the DataTable. When this property is not set, an asterisk is used for the list of fields in the SQL SELECT statement.
    '** Populate the .FilledDataTable with only the
    '** CustID and CustName columns
    dba.SQLSelectList = "CustID, CustName"
SQLString String = String Get/set the entire SQL command string.
    '** Set the entire SQL statement to be used
    '** (The SQL statement can be as complex as
    '** needed)
    dba.SQLString = "SELECT CustID, CustName FROM Customers"
    '** Set the table being queried letting the
    '** parameter of the DatabaseTableName 
    '** property default to False so that the 
    '** passed SQL statement is used instead of
    '** a statement being automatically generated
    dba.DatabaseTableName() = "Customers"
SQLTop N/A = Integer Set the property limiting the number of rows included in the DataTable. The default is zero which means return all rows. The value must be greater than or equal to 0.
SQLWhere String = String Get/set the WHERE expression to be included when the SQL SELECT statement is built. When this property is not set, the WHERE clause is not included in the SELECT command.
    '** Populate the .FilledDataTable with only the
    '** customer having a CustID = 123
    dba.SQLWhere = "CustID = '123'"
TableColumnNames String Array N/A Get the datatable column names. Returned in an unsorted string array.
TableNames String Array N/A Get the names of all tables in the database. Returned in an unsorted string array.

History

This article totally replaces the "VB.NET Class Library: Access Table Interface" article that was posted in May, 2006.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here