Home

Introduction to ADO.NET With Microsoft Access

 

Microsoft Acccess and Visual Studio 2008

 

Introduction

One of the types of databases you can use in an ASP.NET web site is one created using Microsoft Access. You can create the web site using Microsoft Visual Studio, Microsoft Visual Web Developer, or another environment.

 

Introduction to ADO.NET

ADO.NET is a group of libraries used to create powerful databases using various sources that include Microsoft Access, Microsoft Access, Oracle, XML, etc. ADO.NET relies on the .NET Framework's various classes to process requests and perform the transition between a database system and the user. The operations are typically handled through the DataSet class.

While ADO.NET is the concept of creating and managing database systems, the DataSet class serves as an intermediary between the database engine and the user interface.

Getting Access to ADO.NET Libraries

The classes used to create ADO.NET databases are defined in the System.Data namespace and are stored in the System.Data.dll library. If you are creating the application from scratch, remember to reference the System.Data.dll library and add the System.Data namespace in your file(s).

To perform an operation on a database server, you must establish a connection to it.

Characteristics of a Database Connection

 

Introduction

To support a connection to a database server, the .NET Framework provides the OleDbConnection class that is defined in the System.Data.OleDb namespace. Before using this class, you can first include this namespace in your file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

To connect to a database, you can first declare a variable of type OleDbConnection using one of its two constructors. Besides the default constructor, the second constructor takes as argument a string value. Its syntax is:

public OleDbConnection(string connectionString);

You can create the necessary (but appropriate) string in this constructor when declaring the variable. This would be done as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection connection = new OleDbConnection("Something");
    }
}

If you want, you can first create the string that would be used to handle the connection, then pass that string to this construction.

To support the connection as an object, the OleDbConnection class is equipped with the ConnectionString property. If you use the default constructor, you can first define a string value, then assign it to this property.

The Attributes of a Connection String

To use a OleDbConnection object, you must provide various pieces of information joined into a string but are separated from each other with a semi-colon ";". Each piece appears as a Key=Value:

Key1=Value1;Key2=Value2;Key_n=Value_n

It can be passed as follows:

OleDbConnection connection = new OleDbConnection("Key1=Value1;Key2=Value2;Key_n=Value_n");

or assigned as a string to the OleDbConnection.ConnectionString property:

string strConnection = "Key1=Value1;Key2=Value2;Key_n=Value_n";
OleDbConnection connection = new OleDbConnection();

connection.ConnectionString = strConnection;

The Database Provider

To use a database, you must specify its provider. To support this, add an attribute named Provider and assign Microsoft.Jet.OLE.4.0 to it. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection connection =
            new OleDbConnection("Provider=Microsoft.Jet.OLE.4.0;");
    }
}

The Data Source

To use the database, you must indicate its source. To do this, add an attribute named Data Source and assign the file name to it. To help you locate the database file, the Server class of the IIS is equipped with a static method named MapMath. Pass the name of the database or the path to it to this method and assign the whole expression to the Data Source attribute. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection connection =
            new OleDbConnection("Provider=Microsoft.Jet.OLE.4.0;" +
                    "Data Source=" + Server.MapPath("App_Data/exercise.mdb"));
    }
}

Additional Attributes

There are some other attributes you can add, such the as the username and the password, etc.

Opening and Closing a Connection

 

Opening a Connection

After creating a connection string, to apply it and actually establish the connection, you must call the OleDbConnection.Open(). Its syntax is:

public override void Open();

Here is an example of calling it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection connection =
            new OleDbConnection("Provider=Microsoft.Jet.OLE.4.0;" +
                    "Data Source=" + Server.MapPath("App_Data/exercise.mdb"));
        connection.Open();
    }
}

As you can see, this method does not take any argument. The OleDbConnection object that calls it is responsible to get the connection string ready.

Closing a Connection

After using a connection and getting the necessary information from it, you should terminate it. If you are working from a OleDbConnection object, to close a connection, you can call the OleDbConnection.Close() method. Its syntax is:

public virtual void Close();

This method is simply called to close the current connection. Here is an example of calling it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection connection =
            new OleDbConnection("Provider=Microsoft.Jet.OLE.4.0;" +
                    "Data Source=" + Server.MapPath("App_Data/exercise.mdb"));
        connection.Open();

        // Blah Blah Blah

        connection.Close();
    }
}

While you should avoid calling the Open() method more than once if a connection is already opened, you can call the Close() method more than once.

You should always remember to close the connection so that the resources that the database application was using can be made available to other applications. To assist you with this, you can use the using keyword as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (OleDbConnection connection =
            new OleDbConnection("Provider=Microsoft.Jet.OLE.4.0;" +
                    "Data Source=" + Server.MapPath("App_Data/exercise.mdb")))
        {
            connection.Open();

            // Blah Blah Blah
        }
    }
}

When this code executes, it opens the connection. Inside of the curly brackets, you can do whatever you want. When the compiler reaches the closing curly bracket, it calls the OleDbConnection.Close() method, which means you do not need to remember to close it.

Commanding a Database

 

Introduction

After establishing a connection, if you are successful, the database system becomes available to you. An action you perform on the database server or on a database is called a command. 

 
 
 

To support the various commands you can perform on a Microsoft Access database, the System.Data.OleDb namespace provides the OleDbCommand class. To use it, you can declare a variable of type OleDbCommand using one of its constructors.

The Text to Command

The OleDbCommand class is equipped with four constructors. The default constructor allows you to initiate a command without specifying what action would be taken. The action to perform is created as a string statement. This action is represented by the CommandText property of the OleDbCommand class, which is of type string.

 
 

If you want to use the default constructor, you can then create a string that would carry the action to perform. Once the string is ready, you can assign it the CommandText property. This would be done as follow:

OleDbCommand CommandToExecute = new OleDbCommand();
string strCommandToExecute = "Blah Blah Blah";

CommandToExecute.CommandText = strCommandToExecute;

After creating the action that would be performed, you must specify what connection would carry it. To do this, you can first create a OleDbConnection object. To provide it to the command, the OleDbCommand class is equipped with a property named Connection that is of type OleDbConnection. After creating a OleDbConnection object, to provide it to the command, you can assign it to the OleDbCommand.Connection property. This would be done as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (OleDbConnection connection =
            new OleDbConnection("Provider=Microsoft.Jet.OLE.4.0;" +
                    "Data Source=" + Server.MapPath("App_Data/exercise.mdb")))
        {
            OleDbCommand commandToExecute = new OleDbCommand();
            string strCommandToExecute = "Blah Blah Blah";

            connection.Open();
            commandToExecute.Connection = connection;
            commandToExecute.CommandText = strCommandToExecute;
        }
    }
}

Instead of declaring a OleDbCommand variable and the command text separately, as an alternative, you can define the command text when declaring the OleDbCommand variable. You can use the second constructor of the OleDbCommand class. The syntax of this constructor is:

public OleDbCommand(string cmdText);

After using this constructor, you must specify what connection would carry the action. To do this, you can assign a OleDbConnection object to the Connection property of your OleDbCommand

Instead of assigning a OleDbConnection object to the OleDbCommand.Connection property, you can specify what connection would carry the action at the same time you are creating the command. To specify the connection when declaring the OleDbCommand variable, you can use the third constructor of this class. Its syntax is:

public OleDbCommand(string cmdText, OleDbConnection connection);

The second argument to this constructor is an established connection you would have defined. 

If you had initiated the action using the default constructor of the OleDbCommand class, you can assign a OleDbConnection object to the Connection property of the OleDbCommand class.

Command Execution

After establishing a connection and specifying what command needs to be carried, you can execute it. This is done using the ExecuteNonQuery() method of the OleDbCommand class:

public override int ExecuteNonQuery();

This method does not take any argument. The OleDbCommand object that calls it must have prepared a valid command.

Well, the Command Timed Out

The OleDbCommand.CommandTimeOut property allows you to specify the time to wait before trying to execute a command. The default value of this property is 30 (seconds). If you want a different value, assign it to your OleDbCommand variable.

The Type of Command

In this and the next few lessons, all of the commands we perform will be communicated as strings. When we study (stored) procedures, we will see other types of commands. To allow you to specify the type of command you want to perform, the OleDbCommand class is equipped with the CommandType property, which is based on the CommandType enumeration.

The CommandType enumeration has three members: StoredProcedure, TableDirect, and Text. For a OleDbCommand object, the default value is Text.

Reading Data

 

Introduction

A data command is used to initiate an action to perform on a database. To read data of a database, one of the objects you can use is called a data reader. 

The SQL Data Reader

To support data readers, the .NET Framework provides, for a Microsoft Access database, a class named OleDbDataReader. To get a data reader, you can declare a variable of type OleDbDataReader. This class does not have a constructor. This means that, to use it, you must (directly) specify where it would read its data. To provide data to the reader, the OleDbCommand class is equipped with the ExecuteReader() method that is overloaded with two versions. The simplest version of this method uses the following syntax:

public OleDbDataReader ExecuteReader();

Based on this, before using a data reader, you should first create a command that would specify how data would be acquired. Once the data is read, you can pass it to the data reader by assigning the result of a call to a OleDbCommand.ExecuteReader() method to a OleDbDataReader object.

Using a SQL Data Reader

Once data is supplied to the reader, you can access it, one value at a time, from top to bottom. To access data that the reader acquired, you can call its Read() method whose syntax is:

public override bool Read();

As mentioned already, the Read() method simply reads a value and moves on. When reading the values, as mentioned already many times, the data reader reads one value at a time and moves to the next.

 

 

   
 

Home Copyright © 2009-2016, FunctionX, Inc. Next