SqlDataAdapter
的 Fill()
函数执行后,是否会自动关闭 SqlConnection
连接,还是需要手动关闭?
string cnStr = @"Data Source=TEST;Initial Catalog=Suite;Persist Security Info=True;User ID=app;Password=Immmmmm";
cn = new SqlConnection(cnStr);
SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Date", cn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
cn.Close() // ????????
Console.WriteLine(ds.Tables[0].Rows.Count);
Console.WriteLine(cn.State);
using (IDisposable conn = new SqlConnection(), comm = new SqlCommand())
,你不能这样做using (SqlConnection conn = new SqlConnection(), SqlCommand comm = new SqlCommand())
,因此在这种情况下需要三个using
语句。 - Adam Houldsworth