ASP.NET OLEDB Connection
The OleDbConnection instance in ASP.NET is responsible for establishing a connection to a data source, taking the Connection String as an argument during initialization. This connection object is designed to work with the OLEDB Data Provider, enabling seamless communication between the ASP.NET application and the specified data source.
Command Object
Upon establishing the connection between the ASP.NET application and the data source, SQL commands can be executed using the Command Object to retrieve or manipulate data within the database. The Command Object acts in conjunction with the OleDbConnection, allowing for the execution of SQL statements and stored procedures.
VB.Net
Dim connectionString As String
Dim connection As OleDbConnection
connectionString = ConfigurationManager.ConnectionStrings("OLEDbConnection").ToString
connection = New OleDbConnection(connectionString)
C#
string connectionString = ConfigurationManager.ConnectionStrings["OLEDbConnection"].ToString();
OleDbConnection connection = new OleDbConnection(connectionString);
Close() method
Once the required database activities, such as data retrieval or modification, are completed, it is important to close the connection to release resources and maintain efficient data source management. The Close() method provided by the OleDbConnection class serves this purpose, allowing the connection to be closed. By invoking the Close() method, any pending transactions are rolled back, ensuring data integrity, and the connection is released from the database connected via the OLEDB Data Provider.
Copy and paste the following content into your web.config file and provide the parameter values.
web.config
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="OLEDbConnection"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=your database path\yourdatabasename.mdb;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The following ASP.NET program trying to establish connection to a .mdb file and display the message in Label control.
Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
Full Source | C#
using System;
using System.Data ;
using System.Data.OleDb;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["OLEDbConnection"].ToString();
OleDbConnection connection = new OleDbConnection(connectionString);
try
{
connection.Open();
Label1.Text = "Connected to OLEDB Database !!";
connection.Close();
}
catch (Exception ex)
{
Label1.Text = "Could not connect to database " + ex.ToString();
}
}
}
Full Source | VB.NET
Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString As String
Dim connection As OleDbConnection
connectionString = ConfigurationManager.ConnectionStrings("OLEDbConnection").ToString
connection = New OleDbConnection(connectionString)
Try
connection.Open()
Label1.Text = "Connected to OLEDB Database !!"
connection.Close()
Catch ex As Exception
Label1.Text = "Could not connect to database " & ex.ToString
End Try
End Sub
End Class
Conclusion
The OleDbConnection instance in ASP.NET serves as the bridge for establishing and managing a connection to a data source using the OLEDB Data Provider. It enables the execution of SQL commands and facilitates the retrieval and manipulation of data. Properly closing the connection using the Close() method ensures the release of resources and maintains efficient data source management within the ASP.NET application.