ASP.NET ODBC Connection
The OdbcConnection class in ASP.NET serves as a means to establish a connection to a data source using the ODBC (Open Database Connectivity) Data Provider. It is specifically designed to work with ODBC-based data sources.
OdbcConnection class
When creating an instance of the OdbcConnection class, a connection string is passed as an argument to the constructor. This connection string contains the necessary information to establish the connection, such as the data source name, credentials, and other relevant parameters.
VB.Net
Dim connectionString As String
Dim connection As OdbcConnection
connectionString = ConfigurationManager.ConnectionStrings("ODBCConnection").ToString
connection = New OdbcConnection(connectionString)
C#
string connectionString = ConfigurationManager.ConnectionStrings["ODBCConnection"].ToString();
OdbcConnection connection = new OdbcConnection(connectionString);
Command Object
Once the connection is established between the ASP.NET application and the data source, SQL commands can be executed using the Command Object to interact with the database. The Command Object, in combination with the OdbcConnection, enables the execution of SQL statements and stored procedures, facilitating data retrieval or manipulation operations.
After the required database activities, such as querying or updating data, are completed, it is crucial to close the connection to release resources and maintain efficient data source management. The Close() method provided by the OdbcConnection class is used to achieve this. When invoked, the Close() method rolls back any pending transactions, ensuring data integrity, and releases the connection from the database connected via the ODBC Data Provider.
The following ASP.NET program trying to establish connection to an ODBC Data source and display the message in Label control.
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="ODBCConnection"
connectionString="Driver={Microsoft Access Driver (*.mdb)}; DBQ=your database path\yourdatabasename.mdb;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
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.Odbc;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ODBCConnection"].ToString();
OdbcConnection connection = new OdbcConnection(connectionString);
try
{
connection.Open();
Label1.Text = "Connected to ODBC 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.Odbc
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 OdbcConnection
connectionString = ConfigurationManager.ConnectionStrings("ODBCConnection").ToString
connection = New OdbcConnection(connectionString)
Try
connection.Open()
Label1.Text = "Connected to ODBC Database !!"
connection.Close()
Catch ex As Exception
Label1.Text = "Could not connect to database " & ex.ToString
End Try
End Sub
End Class
Conclusion
The OdbcConnection class in ASP.NET allows for establishing connections to data sources using the ODBC Data Provider. It enables the execution of SQL commands and facilitates data retrieval or manipulation. Properly closing the connection using the Close() method ensures the release of resources and maintains efficient data source management within the ASP.NET application.