命中“不能使用已经与其底层RCW分离的COM对象"错误 [英] Hitting "COM object that has been separated from its underlying RCW cannot be used" error

查看:44
本文介绍了命中“不能使用已经与其底层RCW分离的COM对象"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 .NET 4.0 之上编写 Windows 窗体程序并访问 Microsoft Access 数据库.我可以毫无问题地读写,但有时会出现此错误:

<块引用>

不能使用已经与其底层 RCW 分离的 COM 对象.

我尝试使用不同的输入调用此方法 (GetIDBasedonTeamName) 两次(在同一线程上).第二次运行时,我遇到了那个错误.

 OleDbConnection conn = new OleDbConnection();OleDbConnection mDB = new OleDbConnection();OleDbCommand comm = new OleDbCommand();OleDbCommand cmd;OleDbDataReader 博士;public void OpenConnection(string name)//在其他方法中总是先调用这个方法来初始化连接{conn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="+ Application.StartupPath + "\\AppData\\" + 名称 + ".mdb;";conn.Open();comm.Connection = conn;comm.Parameters.Clear();}公共字符串 GetIDBasedonTeamName(string teamName){字符串 toReturn = "";尝试{OpenConnection("表单");comm.CommandText = "从 TeamDetails 中选择 ID,其中 TeamName=@teamName";comm.Parameters.AddWithValue("TeamName", teamName);dr = comm.ExecuteReader();而 (dr.Read()){toReturn = dr[0].ToString();}}捕获 (OleDbException e){字符串错误 = e.Message.ToString();返回空;}最后{}conn.Close();博士关闭();返回返回;}

dr = comm.ExecuteReader(); 发生异常.

调用这个方法的方法里面有两行:

<块引用>

InfoConfig.team1id = Convert.ToInt32(dbm.GetIDBasedonTeamName(cbxTeam1.Text));InfoConfig.team2id = Convert.ToInt32(dbm.GetIDBasedonTeamName(cbxTeam2.Text));

可能是什么原因?我四处阅读,他们提到不要使用不同的线程,但这里是同一个线程.

谢谢,郭红

解决方案

不是只创建一次对象并将它们存储在类中的字段中,而是应该在方法中创建、使用和关闭对象.可能是您最终调用的 Close 释放底层 COM 对象的方法,从而在第二次调用时为您提供异常.

I am trying to write a Windows Form program on top of .NET 4.0 and accessing Microsoft Access Database. I can read and write with no problem but sometimes, I get this error:

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

I tried to call this method (GetIDBasedonTeamName) with different inputs twice (on the same thread). The second time this is run, I got that error.

    OleDbConnection conn = new OleDbConnection();
    OleDbConnection mDB = new OleDbConnection();
    OleDbCommand comm = new OleDbCommand();
    OleDbCommand cmd;
    OleDbDataReader dr;

    public void OpenConnection(string name) // always call this method first in other methods to initialise connection
    {
        conn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="
            + Application.StartupPath + "\\AppData\\" + name + ".mdb;";
        conn.Open();
        comm.Connection = conn;
        comm.Parameters.Clear();
    }

public string GetIDBasedonTeamName(string teamName)
    {
        string toReturn = "";

        try
        {
            OpenConnection("form");
            comm.CommandText = "Select ID from TeamDetails WHERE TeamName=@teamName";
            comm.Parameters.AddWithValue("TeamName", teamName);

            dr = comm.ExecuteReader();

            while (dr.Read())
            {
                toReturn = dr[0].ToString();
            }
        }
        catch (OleDbException e)
        {
            string err = e.Message.ToString();
            return null;
        }
        finally
        {
        }
        conn.Close();
        dr.Close();
        return toReturn;
    }

Exception happened on dr = comm.ExecuteReader();.

The method that was calling this method have this 2 lines inside:

InfoConfig.team1id = Convert.ToInt32(dbm.GetIDBasedonTeamName(cbxTeam1.Text));
InfoConfig.team2id = Convert.ToInt32(dbm.GetIDBasedonTeamName(cbxTeam2.Text));

What could be the cause? I read around and they mentioned not to use different threads but it is the same thread here.

Thanks, Guo Hong

解决方案

Instead of creating the objects only once and storing them in fields in your class you should create, use and close the objects in your method. It is probably the Close you call in the end the method that releases the underlying COM objects giving you the exception on the second call.

这篇关于命中“不能使用已经与其底层RCW分离的COM对象"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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