无法使用asp.net和C#访问Microsoft Access数据库

时间:2012-08-17 14:16:16

标签: c# asp.net ms-access

这是我编写的代码,用于在按钮点击上向手风琴窗格添加一些文字:

protected void Button1_Click1(object sender, EventArgs e)
    {
        //Use a string variable to hold the ConnectionString.
        string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;"
            + "Data Source=D:\\C#Samples\\StudentDetails\\WebRole1\\App_Data\\Students1.accdb";
        System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();
        cn.ConnectionString = connectString;
        //Create an OleDbConnection object, and then pass in the ConnectionString to the constructor.
        //OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString);
        try
        {
            //Open the connection.
            cn.Open();
        }
        catch (Exception ex)
        {
            AccordionPane1.Controls.Add(new LiteralControl("Open Error"));
        }

            string selectString = "SELECT * FROM BasicInfo";

            //Create an OleDbCommand object.
            //Notice that this line passes in the SQL statement and the OleDbConnection object
            OleDbCommand cmd = new OleDbCommand(selectString, cn);

            //Send the CommandText to the connection, and then build an OleDbDataReader.
            //Note: The OleDbDataReader is forward-only.

            try
            {
                OleDbDataReader reader=null;
                try
                {
                    reader = cmd.ExecuteReader();
                }
                catch (Exception es)
                {
                    AccordionPane1.Controls.Add(new LiteralControl(" datareader"));
                }
                string s = "s";
                reader.Read();
                s = reader["S_No"].ToString();

                AccordionPane1.Controls.Add(new LiteralControl(s));
                //Close the reader and the related connection.
                reader.Close();
                cn.Close();
            }
            catch (Exception ex)
            {
                AccordionPane1.Controls.Add(new LiteralControl(" Read Error"));
            }
    }

我在connectString中指定的文件夹中拥有访问2007数据库。当我在浏览器中查看时,在按钮上单击我将获得所有三个例外: enter image description here

打开数据库可能会出现什么问题?我是否需要进行任何其他更改?

3 个答案:

答案 0 :(得分:2)

更改

  

提供商= Microsoft.Jet.OLEDB.4.0;

  

提供商= Microsoft.ACE.OLEDB.12.0

Provider=Microsoft.ACE.OLEDB.12.0;"
            + "Data Source=D:\\C#Samples\\StudentDetails\\WebRole1\\App_Data\\Students1.accdb

希望它能解决问题。

答案 1 :(得分:1)

您的连接字符串可能是问题原因

string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abc.mdb;";

OleDbConnection MyConn = new OleDbConnection(ConnStr);

对于访问权限2007 也请检查数据库的路径是否正确。

答案 2 :(得分:1)

您可以使用|DataDirectory|的{​​{1}} ,而您必须更改real path(由@MMK建议)

Provider=Microsoft.ACE.OLEDB.12.0

并始终使用 string connectString = @"Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|\Students1.accdb;Persist Security Info=False;"; 块正确处理 IDisposable 对象。

using