Friday, February 17, 2012

Connection with MS Access and ASP.NET using C#


This program will show the connection information ASP.Net with Microsoft Access



 Step 1)Add following code in your ASP.Net page
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="oledb.aspx.cs" Inherits="oledb" %>

<!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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <table>
            <tbody>
                <tr>
                    <td>
                        ConnectionString:
                    </td>
                    <td>
                        <asp:Label id="lblOleDbConnectionString" runat="server">ConnectionString</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        ConnectionTimeout:
                    </td>
                    <td>
                        <asp:Label id="lblOleDbConnectionTimeout" runat="server">ConnectionTimeout</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        Database:
                    </td>
                    <td>
                        <asp:Label id="lblOleDbDatabase" runat="server">Database</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        DataSource:
                    </td>
                    <td>
                        <asp:Label id="lblOleDbDataSource" runat="server">DataSource</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        Provider:
                    </td>
                    <td>
                        <asp:Label id="lblOleDbProvider" runat="server">Provider</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        ServerVersion:
                    </td>
                    <td>
                        <asp:Label id="lblOleDbServerVersion" runat="server">ServerVersion</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        State:
                    </td>
                    <td>
                        <asp:Label id="lblOleDbState" runat="server">State</asp:Label></td>
                </tr>
            </tbody>
        </table>
   
    </div>
    </form>
</body>
</html>


Step 2) Add following code in your code back of ASP.Net  page

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Data.OleDb;

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

        OleDbConnection myOleDbConnection = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=D:\\ccc.mdb;");

        myOleDbConnection.Open();

        lblOleDbConnectionString.Text = myOleDbConnection.ConnectionString;
        lblOleDbConnectionTimeout.Text = Convert.ToString(myOleDbConnection.ConnectionTimeout);
        lblOleDbDatabase.Text = myOleDbConnection.Database;
        lblOleDbDataSource.Text = myOleDbConnection.DataSource;
        lblOleDbProvider.Text = myOleDbConnection.Provider;
        lblOleDbServerVersion.Text = myOleDbConnection.ServerVersion;
        lblOleDbState.Text = Convert.ToString(myOleDbConnection.State);

        myOleDbConnection.Close();

    }
}



 

No comments:

Post a Comment