ASP.NET OLEDB Connection
The ASP.NET OleDbConnection instance takes Connection String as argument and pass the value to the Constructor statement. An instance of the ASP.NET OleDbConnection class is supported the OLEDB Data Provider .
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);
When the connection is established betweenASP.NET application and the specified Data Source, SQL Commands will execute with the help of the Command Object and retrieve or manipulate data in the database. Once the Database activities is over Connection should be closed and release from the data source resources .
The Close() method in the OleDbConnection class is used to close the Database Connection. The Close method Rolls Back any pending transactions and releases the Connection from the Database connected by 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
Click the following links to see full source code
C# Source Code
VB.NET Source Code