ACCESS數據庫,有2003、2007版本,不同的版本,鏈接字符也不同,現把代碼黏貼如下:
1、ACCESS2003(.mdb):
private void Form1_Load(object sender, EventArgs e)
{
string ConStr = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source='{0}\test.mdb'", Application.StartupPath);
OleDbConnection oleCon = new OleDbConnection(ConStr);
OleDbDataAdapter oleDap = new OleDbDataAdapter("select * from 賬目", oleCon);
DataSet ds = new DataSet();
oleDap.Fill(ds, "賬目");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
oleCon.Close();
oleCon.Dispose();
}
注釋:測試的是ACCESS2003,擴展名為.mdb。此測試程序在Debug目錄下有一個test.mdb的ACCESS數據庫,數據庫里有一個名為“賬目”的表,將表中的內容顯示在DataGridView控件中;
2、ACCESS2007(*.accdb)
private void Form1_Load(object sender, EventArgs e)
{
string ConStr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source='{0}\test.accdb'", Application.StartupPath);
OleDbConnection oleCon = new OleDbConnection(ConStr);
OleDbDataAdapter oleDap = new OleDbDataAdapter("select * from 賬目", oleCon);
DataSet ds = new DataSet();
oleDap.Fill(ds, "賬目");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
oleCon.Close();
oleCon.Dispose();
}
