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

本文关键字:程序 错误 数据 Net Framework 内部 | 更新日期: 2023-09-27 17:57:31

我正在用Visual Studio 2012 Ultimate版本开发一个WinForm应用程序,该版本包含所有service pack、C#和。NET框架4.5。

我得到这个例外:

Internal .Net Framework Data Provider error 1

使用此堆栈:

   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() 

在析构函数中:

~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;
    }
}

我在this.conn.Close();行得到异常。

connprivate SqlConnection conn = null;

你知道为什么吗?

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

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

基本上可以归结为:

注意事项

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

这不是答案,但我强烈建议您使用using处理连接。那么你就不需要关心处理对象了。

using (SqlConnection connection = new SqlConnection(connectionString)) 
{    
    try    
    {
        connection.Open();
        SqlCommand command = new SqlCommand("......", connection);
        command.ExecuteNonQuery();    
    } 
    catch (Exception) 
    { 
        /*Handle error*/ 
    }
}