Monday 13 August 2012

Connection with MS-Access 2007 with .Net

write below code in your web config file 
<add name="AccessConnectionString" connectionString="Provider=
Microsoft.ACE.OLEDB.12.0;Data Source=G:\Working Project\StickerC\App_Data\Database1.accdb;Persist Security Info=False;Jet OLEDB:Database Password=;" providerName="System.Data.OleDb" />


this is for insert the data in DataBase.
please do not forget to add the name sapce using System.Data.OleDb

  public void InsertData()
    {
      //Establish the coneection
        OleDbConnection myDataConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString);

        // open the data connection.  
        myDataConnection.Open();

        OleDbCommand cmd = myDataConnection.CreateCommand();
        cmd.CommandText = "Insert into tbldata (typeId,data) values (3,'rohit')";
        cmd.ExecuteNonQuery();

        // display the connection's current state open/closed.  
        //Response.Write(string.Format("<i><b>Connection state:</b> {0}</i><br />", myDataConnection.State));

        // close the data connection.  
        myDataConnection.Close();

        // again display the connection's current state open/closed.  
        // Response.Write(string.Format("<i><b>Connection state:</b> {0}</i>", myDataConnection.State));

        // dispose the connection object to release the resources.  
        myDataConnection.Dispose();
    }

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete