vs2010 C#链接 ACCESS数据库

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();
        }



 

 

posted @ 2013-08-09 23:58  jlins  阅读(10336)  评论(0编辑  收藏  举报