Access connections from Visual Studio

Import classes

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

set up your app config

<add name="AccessConnection" 
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\location\accessdb.mdb;
		Persist Security Info=False;;"/>
  public DataTable accessquery()
        {

            OleDbCommand command = new OleDbCommand();
            command.CommandType = CommandType.Text;
            //command.Parameters.AddWithValue("@reportdata", document);
            command.CommandText = "Select * from tablename";
            DataTable dt = new DataTable();
            dt = accessconnection(command);
            return dt;
        }
 private DataTable accessconnection(OleDbCommand command)
        {
            string OLEDBConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString;
            OleDbConnection cns = new OleDbConnection(OLEDBConnectionString);
            DataSet CustomersDataSet = new DataSet();
            OleDbDataAdapter dp = default(OleDbDataAdapter);
            command.Connection = cns;
            cns.Open();
            dp = new OleDbDataAdapter(command);
            dp.Fill(CustomersDataSet);
            cns.Close();
            if (CustomersDataSet.Tables.Count > 0)
            {
                return CustomersDataSet.Tables[0];
            }
            DataTable dt = new DataTable();
            return dt;
        }

Comments are closed.