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

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

我得到这个例外:

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

我在行中得到了exceptionthis.conn.Close();

connprivate SqlConnection conn = null;

你知道为什么吗?

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

基本上它归结为:

警告

不要在类的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*/ } }