有人知道SqlDataAdapter.Dispose方法是否会关闭或处理任何SqlConnection连接吗?我查看了Reflector,发现SqlDataAdapter继承自DbDataAdapter。如果我反汇编并查看该类中的dispose方法,则似乎没有处理任何SqlConnection连接。我想我可以编写一个测试来验证这一点,但我认为我应该先询问是否有人对此有更深入的了解。
.Fill()
和.Update()
函数操作DataTable/DataSet。.Fill()
文档:The connection object associated with the SELECT statement must be valid, but it does not need to be open. If the connection is closed before Fill is called, it is opened to retrieve data, then closed. If the connection is open before Fill is called, it remains open.
根据.Update()
文档,它没有提及连接,因此我希望需要手动管理连接。Dispose()
方法。与Update()
一样,Dispose()
文档没有明确提到连接,因此我期望需要手动关闭它。Using conn as New SqlConnection(""), _
adapter as New SqlDataAdapter() With {.Connection = conn}
' Do stuff
End Using
或者在C#中:
using (SqlConnection conn = new SqlConnection(""))
using (SqlDataAdapter adapter = new SqlDataAdapter() {Connection = conn})
{
// Do stuff
}
Using conn as New SqlConnection("")
Using adapter as New SqlDataAdapter() With {.Connection = conn}
'Do stuff'
End Using
End Using
如果您选择的编程语言是C#,那么其语法几乎完全相同。
不,它不会释放连接。我认为它不应该这样做。你可能想在其他地方使用它。