无法使用已与其基础RCW分离的COM对象。 [英] COM object that has been separated from its underlying RCW cannot be used.

查看:52
本文介绍了无法使用已与其基础RCW分离的COM对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            connect.ConnectionString = Tool.getStringDB(set.dbname, set.versionaccess);
            connect.Open();


            cmd.Connection = connect;
            cmd.CommandText = "SELECT * From InvoiceInformation WHERE Order_date BETWEEN @StartDate AND @StopDate";
            String sdate = startdate.Value.ToString("yyyy-MM-dd");
            String edate = enddate.Value.ToString("yyyy-MM-dd");
            cmd.Parameters.AddWithValue("@StartDate", startdate);
            cmd.Parameters.AddWithValue("@StopDate", edate);


            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                dataGridView1.Rows.Add();

                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Invoice_no"].Value = reader[0].ToString();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Purchase_ProductID"].Value = reader[1].ToString();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Purchase_ProductName"].Value = reader[2].ToString();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Purchase_Amout"].Value = reader[3].ToString();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Purchase_TotalPay"].Value = reader[4].ToString();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Order_date"].Value = reader[5].ToString();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Customer_name"].Value = reader[6].ToString();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Address"].Value = reader[7].ToString();
            }
            /*productseq.Text = dataGridView1.Rows[0].Cells["ID"].Value.ToString();
            productid.Text = dataGridView1.Rows[0].Cells["Product_ID"].Value.ToString();
            productname.Text = dataGridView1.Rows[0].Cells["Product_Name"].Value.ToString();
            productunit.Text = dataGridView1.Rows[0].Cells["Unit"].Value.ToString();
            productprice.Text = dataGridView1.Rows[0].Cells["Price"].Value.ToString();
            productstored.Text = dataGridView1.Rows[0].Cells["Stored"].Value.ToString();*/
            connect.Close();
        }





错误





Error at

OleDbDataReader reader = cmd.ExecuteReader();



处理了InvalidComObjectException

已与其基础RCW分离的COM对象不能使用。





如何解决?


InvalidComObjectException was Handled
COM object that has been separated from its underlying RCW cannot be used.


How to fix ?

推荐答案

而不是数据库连接和命令的全局变量,使用局部变量。

我建议使用块将它们嵌入块:

Instead of global variables for the database connection and command, use local variables.
I suggest to embed them in using blocks:
using(OleDbConnection connect = new OleDbConnection())
{
    ...
    using(OleDbCommand cmd = ...)
    {
        ....
        using (OleDbDataReader reader = ..)
        {
            ....
        }
    }
}


这篇关于无法使用已与其基础RCW分离的COM对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆