How to connect access database in c# [closed] Ask Question
I have access database file with 7
tables in it but I don't know how to
8 connect and show all tables, If some
one can help me?
this is my code but it doesn't show
anything
6
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
String connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\\Tables.accdb;Persist Security Info=True";
string sql = "SELECT Clients FROM Tables";
conn.ConnectionString = connection;
conn.Open();
DataSet ds = new DataSet();
DataGridView dataGridView1 = new DataGridView();
BindingSource bSource = new BindingSource();
OleDbDataAdapter adapter = new OleDbDataAdapter(sql,conn);
adapter.Fill(ds);
//conn.Close();
dataGridView1.DataSource = ds;
c# .net ado.net
edited Jun 10 '13 at 12:28
Mike Perrenoud
54.6k 20 125 203
asked Jun 10 '13 at 12:26
user2386687
54 1 1 5
closed as not a real question by
Neolisk, George Duckett,
jcwenger, Anthony Grist, rael_kid
Jun 10 '13 at 14:53
It's difficult to tell what is being asked
here. This question is ambiguous,
vague, incomplete, overly broad, or
rhetorical and cannot be reasonably
answered in its current form. For help
clarifying this question so that it can be
reopened, visit the help center.
If this question can be reworded to fit
the rules in the help center, please edit
the question.
And what is the error this code is
throwing? Please edit the question
with that information. –
Mike Perrenoud Jun 10 '13 at 12:27
Have you seen
social.msdn.microsoft.com/Forums/en
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
-US/csharpgeneral/thread/…? –
Terms of Service.
L-Four Jun 10 '13 at 12:29
Use
Provider=Microsoft.Jet.OLEDB.4.0
instead? – Alvin Wong Jun 10 '13 at
12:30
@AlvinWong - That would mean his
software would not function as a 64-bit
process since that provider is not
supported on 64-bit operating
systems. – Security Hound Jun 10 '13
at 12:49
I used
Provider=Microsoft.Jet.OLEDB.4.0 it
doesn't work and then I tried 12.0. –
user2386687 Jun 10 '13 at 14:29
2 Answers
Try this code,
15 public void ConnectToAccess()
{
System.Data.OleDb.OleDbConnection
System.Data.OleDb.OleDbConnec
// TODO: Modify the connection st
// additional required properties
conn.ConnectionString = @"Provide
@"Data source= C:\Documents a
@"My Documents\AccessFile.mdb
try
{
conn.Open();
// Insert code to process dat
}
catch (Exception ex)
{
MessageBox.Show("Failed to co
}
finally
{
conn.Close();
}
}
http://msdn.microsoft.com/en-
us/library/5ybdbtte(v=vs.71).aspx
answered Jun 10 '13 at 12:30
Chamika Sandamal
19.7k 3 52 76
1 That only works for Access Databases
older than Access 2007 –
Fandango68 Jan 20 '18 at 3:31
You are building a DataGridView on
the fly and set the DataSource for it.
4 That's good, but then do you add the
DataGridView to the Controls
collection of the hosting form?
By using our site,this.Controls.Add(dataGridView1);
you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.
By the way the code is a bit confused
String connection = "Provider=Microso
Source=|DataDirectory|\\Tables.accdb;
string sql = "SELECT Clients FROM T
using(OleDbConnection conn = new OleD
{
conn.Open();
DataSet ds = new DataSet();
DataGridView dataGridView1 = new
using(OleDbDataAdapter adapter =
{
adapter.Fill(ds);
dataGridView1.DataSource = d
// Of course, before addint
// set position, location an
// Why don't you create the
instead?
this.Controls.Add(dataGridVi
}
}
EDIT After the comments below it is
clear that there is a bit of confusion
between the file name
(TABLES.ACCDB) and the name of
the table CLIENTS.
The SELECT statement is defined (in
its basic form) as
SELECT field_names_list FROM _tablen
so the correct syntax to use for
retrieving all the clients data is
string sql = "SELECT * FROM Clients
where the * means -> all the fields
present in the table
edited Jun 10 '13 at 14:56
answered Jun 10 '13 at 12:35
Steve
184k 16 163 223
Thank you Steve, Im having an error
adaper.Fill(ds); Th Microsoft Access
Database engine cannot fine the
imput table or query 'Tables'.Make
sure it exist and that its name is
spelled correctly. – user2386687 Jun
10 '13 at 14:26
The error seems clear to me. Do you
have a table named Tables in your
Tables.accdb? Or are you confusing
the name of the file for the datatable?
– Steve Jun 10 '13 at 14:30
The tables is right but I'm thinking,
when I try to connect client table from
file in select Clients from Tables
something I'm doing wrong, oh I don't
know this stuff. Even when I try to
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
explain I don't know very well –
Terms of Service. user2386687 Jun 10 '13 at 14:39
If you open your Tables.accdb file with
MS-Access do you see a table names
Tables or do you see a table named
Clients ? – Steve Jun 10 '13 at
14:42
file name in Access database is
'Tables' and it has 7 tables Clients,
Tasks, Projects, Developers etc. –
user2386687 Jun 10 '13 at 14:47
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.