C# .NET - access

Asked By Bhanuprakash T.R on 02-Jul-11 06:14 AM

        hi sir,

             i want to connect the database acess to c#.net ... please send example coding for that
Kirtan Patel replied to Bhanuprakash T.R on 02-Jul-11 06:18 AM
OledbConnection con = new OledbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\mydatabase.mdb;");
con.Open();
OledbCommand comm = new OledbCommand("select * from Categories", con);
OledbDataAdapter da = new OledbDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
dataGridView1.DataSource = dt;
Reena Jain replied to Bhanuprakash T.R on 02-Jul-11 06:23 AM
Hi,

You can use the OleDbConnection and OleDbCommand objects to connect to an MS Access database and insert some records:

OleDbConnection oleDbConnection = new OleDbConnection(" Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\db1.mdb");

OleDbCommand oleDbCommand = new OleDbCommand();
 
oleDbCommand.Connection = oleDbConnection;
oleDbCommand.CommandText = "INSERT INTO YourTable ( Col1, Col2 ) VALUES (1, 'ABC')";
 
oleDbConnection.Open();
oleDbCommand.ExecuteNonQuery();
oleDbConnection.Close();

Hope this will help you
ledan -OSAMA BIN Ledan replied to Kirtan Patel on 02-Jul-11 06:25 AM
how u do like that.......
hari hari replied to Bhanuprakash T.R on 02-Jul-11 07:52 AM
.mdb or .accdb?
Riley K replied to Bhanuprakash T.R on 02-Jul-11 12:24 PM
For connecting to MS Access you need to use Oledb namespace

using System.Data.OleDb;

Specify the Connection string

private string conStr = @"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source=contact.md
b;Jet OLEDB:Database Password=123456";


static void Main()
    {
      try
      {
        con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source=contact.mdb;Jet OLEDB:Database Password=123456");  // connection string change database name and password here.
        con.Open(); //connection must be openned
        OleDbCommand cmd = new OleDbCommand("SELECT * from contact", con); // creating query command
        OleDbDataReader reader = cmd.ExecuteReader(); // executes query
        while(reader.Read()) // if can read row from database
        {
  
          Console.WriteLine(reader.GetValue(1).ToString() + " = " + reader.GetValue(3).ToString());  // Get column 1 and column 3 value and print
        }
      }
      catch(Exception ex)
      {
        Console.WriteLine("Ex: "+ex);  // shows exception message in console if any errors occured
      }
      finally
      {
        con.Close();  // finally closes connection
      }
    }

Regards
Jitendra Faye replied to Bhanuprakash T.R on 04-Jul-11 07:52 AM
For that you have to use Connection string with details of Access database.

Here you can get connection string to Connect to Access-

http://www.connectionstrings.com/access


use this code-

protected void GetDetails_Click(object sender, EventArgs e)
    {

 SqlConnection con = new SqlConnection("con string to connect with access ");
 SqlDataAdapter da;
 string mySQL = "SELECT *  FROM emptable where empid='" + TxtEmpId.Text  + "' ";
 da = new SqlDataAdapter(mySQL, con);
 con.Open();
 DataSet ds = new DataSet();
 da.Fill(ds);

  //Now Filling Details-

  TxtEmpId.Text= ds.Tables[0].Rows[0][0].ToString();     //for id
  TxtEmpName.Text= ds.Tables[0].Rows[0][1].ToString();   //for name
}

Try this code and let me know.