可是看msdn上的 // Dispose(bool disposing) executes in two distinct scenarios. // If disposing equals true, the method has been called directly // or indirectly by a user's code. Managed and unmanaged resources // can be disposed. // If disposing equals false, the method has been called by the // runtime from inside the finalizer and you should not reference // other objects. Only unmanaged resources can be disposed. protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if(!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if(disposing) { // Dispose managed resources. component.Dispose(); } // Call the appropriate methods to clean up // unmanaged resources here. // If disposing is false, // only the following code is executed. CloseHandle(handle); handle = IntPtr.Zero; // Note disposing has been done. disposed = true; } } 好像是托管资源也要释放~~~~~
using结束后会自动调用对象的Dispose方法来释放资源
所以一般都是using(var c = new SqlConnection(...)){}
另外,一般异步都直接开线程来做,还是看你帖子才知道有BeginExecuteNonQuery,回去研究一下
谢谢。
还想问下
“SqlConnection最好不要保存在类的实例变量中”是什么意思,如果是一个数据库操作的类,可能里面有
有执行SQL指令之类的方法,方法里面应该会用到SqlConnection的
using (SqlConnection conn = new SqlConnection(...))
{
//各种使用Conn
}
其次,类实现 IDisposable 接口并在接口函数Dispose()中释放所占用的资源。这样可以让我们在一个确定的时刻释放资源(非内存资源,比如文件句柄、数据库连接等等),同时一个好处就是我们定义的类就支持using语句了(就像我上面的写法)。这个可以参考一下MSDN的这篇文章IDisposable
才声明,
可是那个类把SqlCommand,SqlDataAdapter这些都全部作为函数里面的一个临时的对象,这样当频繁地使用这些函数时是不是就产生了很多垃圾,这样是不是应该把SqlCommand、SqlDataAdapter这些变量作为类的成员变量
看看下面这个http://developer.51cto.com/art/200909/151182.htm
//打开数据库连接,注意在执行SQL指令前调用此方法
private void Open()
{
if (!isOpen)
{
sqlConnection = new SqlConnection(ConnectionString);
try
{
sqlConnection.Open();
sqlCommand = new SqlCommand(ConnectionString);
isOpen = true;
}
catch (InvalidOperationException ex)
{
MessageBox.Show(ex.ToString());
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
} //关闭数据库连接
public void Close()
{
Dispose(false);
}
//当对象不再使用时应调用此方法
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} private void Dispose(bool disposing)
{
if (!this.disposed)
{
//清理资源
if (disposing)
{
sqlCommand.Dispose();
DataAdapter.Dispose();
} //关闭连接
if (isOpen && sqlConnection != null)
{
try
{
sqlConnection.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
this.disposed = true;
isOpen = false;
}
}
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// Dispose managed resources.
component.Dispose();
} // Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero; // Note disposing has been done.
disposed = true; }
}
好像是托管资源也要释放~~~~~
前者是释放资源,后者关闭连接;至于能否省略Dispose(),不清楚