c#访问OleDB错误80004005

时间:2019-05-17 10:01:17

标签: c# ms-access-2010 oledb

我正在使用具有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中执行查询,则运行正常。
任何想法都非常欢迎。
谢谢。

1 个答案:

答案 0 :(得分:3)

this之间列出了 Section Number Container 一词。您不应该在表模式中使用它们,但如果您确实无法将这些名称更改为其他名称,则需要将它们放在方括号之间

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
}