内部.Net Framework数据提供程序错误1

Van*_*nel 13 .net c# ado.net destructor

我正在使用Visual Studio 2012 Ultimate版本开发一个带有所有Service Pack,C#和.NET Framework 4.5的WinForm应用程序.

我得到这个例外:

Internal .Net Framework Data Provider error 1
Run Code Online (Sandbox Code Playgroud)

有了这个堆栈:

   en System.Data.ProviderBase.DbConnectionInternal.PrePush(Object expectedOwner)
   en System.Data.ProviderBase.DbConnectionPool.PutObject(DbConnectionInternal obj, Object owningObject)
   en System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
   en System.Data.SqlClient.SqlConnection.CloseInnerConnection()
   en System.Data.SqlClient.SqlConnection.Close()
   en AdoData.TRZIC.DisposeCurrentConnection() 
   en AdoData.TRZIC.Finalize() 
Run Code Online (Sandbox Code Playgroud)

在析构函数中:

~TRZIC()
{
    DisposeCurrentConnection();

    if (this.getCodeCmd != null)
        this.getCodeCmd.Dispose();
}

private void DisposeCurrentConnection()
{
    if (this.conn != null)
    {
        if (this.conn.State == ConnectionState.Open)
            this.conn.Close();

        this.conn.Dispose();
        this.conn = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到了例外this.conn.Close();.

而且connprivate SqlConnection conn = null;

你知道为什么吗?

Van*_*nel 17

我在这里找到了解决方案.

基本上它归结为:

警告

不要在类的Finalize方法中对Connection,DataReader或任何其他托管对象调用Close或Dispose.在终结器中,您应该只释放您的类直接拥有的非托管资源.如果您的类不拥有任何非托管资源,请不要在类定义中包含Finalize方法.有关更多信息,请参阅垃圾收集.