c# 访问 OleDB 错误 80004005

c# Access OleDB err 80004005

我正在开发一个带有 Access 2010 数据库连接的应用程序,但我一直收到 OleDB 错误 80004005,我不明白为什么。

            const String conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\OneDrive\Dropbox\SharpDevelop Projects\electronics inventory\electronics.mdb";
    const String qCont = "select Section, Number, Stock from Container where Component = @IdComp order by Section, Number";

    int oldParamSubcat = 0;
    OleDbConnection connection = new OleDbConnection(conn);

    void GrdCompCellClick(object sender, DataGridViewCellEventArgs e)
    {
        String IdComp = grdComp[grdComp.Columns["ID"].Index, grdComp.CurrentCell.RowIndex].Value.ToString();
        try
        {
            grdSubcat.DataSource = null;
            grdSubcat.Rows.Clear();
            grdSubcat.Columns.Clear();
            connection.Open();
            OleDbCommand cmdDetail = new OleDbCommand();
            cmdDetail.Connection = connection;
            cmdDetail.CommandText = qDetail;
            cmdDetail.Parameters.AddWithValue("@IdComp", Convert.ToInt32(IdComp));

            txtDetails.Text = "";
            OleDbDataReader rdDetail = cmdDetail.ExecuteReader();

            rdDetail.Read();
            txtDetails.Text = rdDetail["Component"].ToString() + "\r\n";
            txtDetails.Text += rdDetail["Parameter"].ToString() + ": ";
            txtDetails.Text += rdDetail["Val"].ToString() + "\r\n";

            while(rdDetail.Read())
            {
                txtDetails.Text += rdDetail["Parameter"].ToString() + ": ";
                txtDetails.Text += rdDetail["Val"].ToString() + "\r\n";
            }

            rdDetail.Close();
            connection.Close();
            connection.Open();

            OleDbCommand cmdCode = new OleDbCommand();
            cmdCode.Connection = connection;
            cmdCode.CommandText = qCode;
            cmdCode.Parameters.AddWithValue("@IdComp", Convert.ToInt32(IdComp));

            txtDetails.Text += "\r\n";
            OleDbDataReader rdCode = cmdCode.ExecuteReader();

            while(rdCode.Read())
            {
                txtDetails.Text += rdCode["Seller"].ToString() + ": ";
                txtDetails.Text += rdCode["Code"].ToString() + "\r\n";
            }

            rdCode.Close();
            connection.Close();
            connection.Open();

            OleDbCommand cmdCont = new OleDbCommand();
            cmdCont.Connection = connection;
            cmdCont.CommandText = qCont;
            cmdCont.Parameters.AddWithValue("@IdComp", Convert.ToInt32(IdComp));

            txtDetails.Text += "\r\n";
            OleDbDataReader rdCont = cmdCont.ExecuteReader(); ////////// here is where i receive the error ///////////////

            while(rdCont.Read())
            {
                txtDetails.Text += "Container: ";
                txtDetails.Text += rdCont["Section"].ToString() + "-";
                txtDetails.Text += rdCont["Number"].ToString() + " = ";
                txtDetails.Text += rdCont["Stock"].ToString() + " units\r\n";
            }

            rdCont.Close();
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    } 

其余代码工作正常,我只在 cmdCont.ExecuteReader();
上遇到错误 The error message
如果我在 Access 中执行查询,它运行正常。
非常欢迎任何想法。
谢谢

单词SectionNumberContainer列在[=12=之间].你不应该在你的 table 架构中使用它们,但如果你真的不能将这些名称更改为不同的名称,那么你需要将它们放在方括号

之间
const String qCont = @"select [Section], [Number], Stock from [Container]
                       where Component = @IdComp order by [Section], [Number]";

此外,您应该对连接、命令和阅读器等一次性对象使用更稳健的方法。尝试以这种方式将 using 语句添加到您的代码中:

try
{
    ....
    using(OleDbConnection connection = new OleDbConnection(......))
    {
         connection.Open();
         ....
         string cmdText = "yourdetailquery";
         using(OleDbCommand cmdDetail = new OleDbCommand(cmdText, connection))
         {
             .... // parameters
             using(OleDbDataReader rdDetail = cmdDetail.ExecuteReader())
             {
               ... read detail data .... 
             }
         }
         // here the rdDetail is closed and disposed, 
         // you can start a new reader without closing the connection
         cmdText = "yourcodequery";
         using(OleDbCommand cmdCode = new OleDbCommand(cmdText, connection))
         {
             .... parameters
             using(OleDbReader rdCode = cmdCode.ExecuteReader())
             {
                 // read code data...
             }
         }
         ... other command+reader
   }
   // Here the connection is closed and disposed
}
catch(Exception ex)
{
    // any error goes here with the connection closed
}