This is just a thought of providing some code snippet for those who works on small project's and basically deals with Microsoft Access Database. You can use normal T-SQL statements with OleHelper class. It does not support for stored procedures.
Here is the detail code:
Public NotInheritable Class OleHelper
#Region "private utility methods & constructors"
Private Sub New()
End Sub
#End Region
#Region "ExecuteDataSet"
''' <summary>
''' Execute a OleCommand (that returns a resultset and takes no parameters) against the provided OleDbTransaction.
''' </summary>
''' <remarks>
''' e.g.:
''' DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
''' </remarks>
''' <param name="transaction">a valid OleDBTransaction</param>
''' <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
''' <param name="commandText">the stored procedure name or T-SQL command</param>
''' <returns>a dataset containing the resultset generated by the command</returns>
Public Shared Function ExecuteDataset(ByVal connection As OleDbConnection, ByVal transaction As OleDbTransaction, ByVal commandType As CommandType, ByVal commandText As String) As DataSet
'return ExecuteDataset(transaction, commandType, commandText);
'create a command and prepare it for execution
Dim cmd As New OleDbCommand()
PrepareCommand(cmd, connection, transaction, commandType, commandText)
'create the DataAdapter & DataSet
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet()
'fill the DataSet using default values for DataTable names, etc.
da.Fill(ds)
cmd.Parameters.Clear()
'return the dataset
Return ds
End Function
#End Region
#Region "ExecuteNonQuery"
Public Shared Function ExecuteNonQuery(ByVal connection As OleDbConnection, ByVal transaction As OleDbTransaction, ByVal commandType As CommandType, ByVal commandText As String) As Integer
'create a command and prepare it for execution
Dim cmd As New OleDbCommand()
PrepareCommand(cmd, connection, transaction, commandType, commandText)
'finally, execute the command.
Dim retval As Integer = cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Return retval
End Function
#End Region
#Region "PrepareCommand"
Private Shared Sub PrepareCommand(ByVal command As OleDbCommand, ByVal connection As OleDbConnection, ByVal transaction As OleDbTransaction, ByVal commandType As CommandType, ByVal commandText As String)
'if the provided connection is not open, we will open it
If connection.State <> ConnectionState.Open Then
connection.Open()
End If
'associate the connection with the command
command.Connection = connection
'set the command text (stored procedure name or SQL statement)
command.CommandText = commandText
'if we were provided a transaction, assign it.
If transaction IsNot Nothing Then
command.Transaction = transaction
End If
'set the command type
command.CommandType = commandType
Return
End Sub
#End Region
End Class
In my project I have "Category.mdb" database and in this I have "Tbl_Categories" table which is as below:
catId AutoNumber
catName Text
catDescription Text
addedBy Number
catStatus Text
sysTime Text
Where catId is a primary key.
On Default.aspx page I am having form which accepts the input values.
Here is a code which I can use to add new record by instantiating "OleHelper".
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.OleDb
Partial Public Class _Default
Inherits System.Web.UI.Page
Private oleCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:\Web\DbHelper\App_Data\Category.mdb'")
Private objTrans As OleDbTransaction = Nothing
Private retInsVal As Integer = 0
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
'Here is how we can create object and pass different parameters to the method
If (txtCatName.Text.Trim() <> "") AndAlso (txtDescription.Text.Trim() <> "") Then
'do Db activity
Try
oleCon.Open()
objTrans = oleCon.BeginTransaction()
Dim strChkVal As String = ""
If chkStatus.Checked = True Then
strChkVal = "Y"
Else
strChkVal = "N"
End If
retInsVal = DbHelper.OleHelper.ExecuteNonQuery(oleCon, objTrans, CommandType.Text, "INSERT INTO Tbl_Categories (catName,catDescription,addedBy,catStatus,sysTime) VALUES('" + txtCatName.Text.Replace("'", "''").Trim() + "','" + txtDescription.Text.Replace("'", "''").Trim() + "', 1,'" + strChkVal + "','" + DateTime.Now.ToLongDateString() + "')")
objTrans.Commit()
lblMessage.Text = "New record added successfully!"
Catch
lblMessage.Text = "An error hsa occured while adding new category!"
objTrans.Rollback()
Finally
'free the resources
oleCon.Close()
End Try
End If
End Sub
End Class
I am sure this code will help developer for maintaining two-tier architecture where one can have this class as DataAcccess Layer and aspx files as presentation layer. You can add some more methods to this class like ExecuteScaler() etc. and can extend this article on some project.
Note: In this article I have my project location at "d:\\web\\...\\Category.mdb so in this case my connection becomes as:
Dim oleCon As New OleDbConnection("Provider= Microsoft.Jet.OLEDB.4.0;Data Source='D:\Web\DbHelper\App_Data\Category.mdb'")
In your case it will differ, you can very well store this provider in web.config file or you can use Server.Mappath() to point your database location.
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# Corner (http://www.c-sharpcorner.com/).