c#如何连接access数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17023861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect access database in c#
提问by user2386687
I have access database file with 7 tables in it but I don't know how to connect and show all tables, If some one can help me?
我有 7 个表的访问数据库文件,但我不知道如何连接和显示所有表,如果有人可以帮助我?
this is my code but it doesn't show anything
这是我的代码,但它没有显示任何内容
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;
回答by Chamika Sandamal
Try this code,
试试这个代码,
public void ConnectToAccess()
{
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
// TODO: Modify the connection string and include any
// additional required properties for your database.
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= C:\Documents and Settings\username\" +
@"My Documents\AccessFile.mdb";
try
{
conn.Open();
// Insert code to process data.
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
finally
{
conn.Close();
}
}
http://msdn.microsoft.com/en-us/library/5ybdbtte(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/5ybdbtte(v=vs.71).aspx
回答by Steve
You are building a DataGridView on the fly and set the DataSource for it. That's good, but then do you add the DataGridView to the Controls collection of the hosting form?
您正在动态构建 DataGridView 并为其设置 DataSource。这很好,但是您是否将 DataGridView 添加到托管表单的 Controls 集合中?
this.Controls.Add(dataGridView1);
By the way the code is a bit confused
顺便说一下代码有点乱
String connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Tables.accdb;Persist Security Info=True";
string sql = "SELECT Clients FROM Tables";
using(OleDbConnection conn = new OleDbConnection(connection))
{
conn.Open();
DataSet ds = new DataSet();
DataGridView dataGridView1 = new DataGridView();
using(OleDbDataAdapter adapter = new OleDbDataAdapter(sql,conn))
{
adapter.Fill(ds);
dataGridView1.DataSource = ds;
// Of course, before addint the datagrid to the hosting form you need to
// set position, location and other useful properties.
// Why don't you create the DataGrid with the designer and use that instance instead?
this.Controls.Add(dataGridView1);
}
}
EDITAfter 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 statementis defined (in its basic form) as
编辑在下面的评论之后,很明显文件名 (TABLES.ACCDB) 和表 CLIENTS 的名称之间存在一些混淆。
SELECT 语句(以其基本形式)定义为
SELECT field_names_list FROM _tablename_
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
其中*手段 -> 表中存在的所有字段

