日期:2014-05-18  浏览次数:20785 次

C#连接ACCESS数据库
C#编写的控制台应用程序,如何打开连接ACCESS数据库,我看了许多网上的方法用了都不对,有许多问题也不懂,忘高手给指点指点啊!

------解决方案--------------------
C# code

void OpenFile(string fileName)
        {
            //创建数据库连接
            string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName;
            OleDbConnection aConnection = new OleDbConnection(strconn);
            //创建command对象并保存sql查询语句
            OleDbCommand aCommand = new OleDbCommand("select * from GeoXY", aConnection);
            try
            {
                aConnection.Open();
                //创建datareader 对象来连接到表单
                OleDbDataReader aReader = aCommand.ExecuteReader();
                //循环遍历数据库
                while (aReader.Read())
                {
                    .....
                }
                aReader.Close();
                aConnection.Close();
            }
            catch(OleDbException e)
            {
                MessageBox.Show(e.Message);
            }
        }