C# : Tutorial of Asp.net with c# and example

Example and code about windows application, web application, SQL, C#, WPF etc which is helpful to developer and programer.

Search This Blog

Code For do connection with ms access data base and explain concept with example and connection string using oledb connection.

♠ Posted by asp blogger in ,,,, at Saturday, January 31, 2015


In This Article we explain about Data Base Connection with MS Access Data Base 2007


Use Of Data Base connection: 
Data base connection is use for retrieve, insert, update, delete data from database by any system or software.


Following step for establish connection with Ms Access Data Base 2007
Step 1- Put following code in web.config file of asp.Net website

  
<connectionStrings>
    <add name="myconnectionstring" providerName="System.Data.OleDb" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\DB\Database1.accdb"/>
  </connectionStrings>

Step 2- Import Following two package in your code Behind (.aspx.cs) file where you want to do connection

using System.Data.OleDb;
using System.Configuration;

Step 3- We Do connection on load of page so put following code Page_load Event code Behind (.aspx.cs) file where you want to do connection.

OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
        try
        {
            conn.Open();
            msglitrl.Visible = true;
            msglitrl.Text = "Connection Done Successfully";
        }
        catch
        {

            msglitrl.Visible = true;
            msglitrl.Text = "Connection Fail.";
  }

Summary:

In Web.Config file

<connectionStrings>
    <add name="myconnectionstring" providerName="System.Data.OleDb" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\DB\Database1.accdb"/>
  </connectionStrings>

In code behind 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;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
        try
        {
            conn.Open();
            msglitrl.Visible = true;
            msglitrl.Text = "Connection Done Successfully";
        }
        catch
        {

            msglitrl.Visible = true;
            msglitrl.Text = "Connection Fail.";
        }
   }


0 comments:

Post a Comment