【问题标题】:c# Access OleDB err 80004005c#访问OleDB err 80004005
【发布时间】:2019-05-17 18:13:10
【问题描述】:

我正在开发一个具有 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 中执行查询,它运行正常。
非常欢迎任何想法。
谢谢。

【问题讨论】:

  • 为什么要关闭并重新打开连接?
  • 您确定这些信息之间没有关系吗?也许您不需要使用三个不同的查询来从您的数据库中获取数据。
  • @GolezTrol 我正在关闭并重新打开,因为在某些情况下,在同一连接上打开第三个阅读器会导致错误(不知道为什么)。

标签: c# ms-access-2010 oledb


【解决方案1】:

SectionNumberContainer这几个词列在reserved keyword for MS-Access之间。您不应该在表架构中使用它们,但如果您真的无法将这些名称更改为不同的名称,那么您需要将它们放在方括号之间

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
}

【讨论】:

  • 谢谢。我知道“部分”和“编号”(并相应地更改了它们)。但是我没有意识到“容器”也被保留了。
  • 我也是。 MS-Access 的这些怪癖有时真的很烦人
猜你喜欢
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-02
相关资源
最近更新 更多