通过datagridview显示access数据库中内容(附代码)
private void button1_Click(object sender, EventArgs e){
try
{ // 利用 OleDbConnectionStringBuilder 对象来构建
// 连接字符串。
OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
connectStringBuilder.DataSource = @"D:\temp\db1.mdb";
connectStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
using (OleDbConnection cn = new OleDbConnection(connectStringBuilder.ConnectionString))
{
DataSet ds = new DataSet();
OleDbCommand cmdLiming = new OleDbCommand("SELECT * FROM cade", cn);
cn.Open();
using (OleDbDataReader drLiming = cmdLiming.ExecuteReader())
{
ds.Load(drLiming, LoadOption.OverwriteChanges, new string[] { "cade" });
// 将 BindingSource 组件绑定到 DataSet 当中的"cade"数据表。
this.BindingSource1.DataSource = ds.Tables["cade"];
// 将 DataGridView 控件绑定到 BindingSource 组件。
this.DataGridView1.DataSource = this.BindingSource1;
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
}
大家帮我看看问题出在哪?谢谢