0

これは、ボタンのクリック時にアコーディオン ペインにテキストを追加するために書いたコードです。

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 で指定したフォルダに Access 2007 データベースがあります。ブラウザで表示しているときに、ボタンをクリックすると、3 つの例外がすべて発生します。 ここに画像の説明を入力

データベースを開く際に何が問題になる可能性がありますか? 他の変更を加える必要がありますか?

4

3 に答える 3

2

変化する

プロバイダー = Microsoft.Jet.OLEDB.4.0;

Provider=Microsoft.ACE.OLEDB.12.0

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

うまくいけば、それは問題を解決します。

于 2012-08-17T14:22:16.570 に答える
1

|DataDirectory| 代わりに使用できreal path、変更する必要がありますProvider=Microsoft.ACE.OLEDB.12.0(@MMKの提案による)

 string connectString = @"Microsoft.ACE.OLEDB.12.0;
       Data Source=|DataDirectory|\Students1.accdb;Persist Security Info=False;";

IDisposableオブジェクトを適切に破棄するusingブロックを常に使用します。

using(OleDbConnection cn=new OleDbConnection())
{
 using(OleDbCommand cmd=new OleDbCommand())
 {
  cn.ConnectionString=connectionString;
  cmd.CommandText=selectString;
  cmd.Connection=cn;
  ...
 }
}
于 2012-08-17T14:24:57.950 に答える
1

接続文字列が問題の原因である可能性があります

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

OleDbConnection MyConn = new OleDbConnection(ConnStr);

thisアクセス 2007 についても、データベースのパスが正しいことを確認します。

于 2012-08-17T14:22:53.603 に答える