In your current use it will close for you:
If the IDbConnection closes before calling Fill, it opens to retrieve the data and then closes. If the connection is open before calling Fill, it remains open.
http://msdn.microsoft.com/en-us/library/zxkb3c3d.aspx
I think it is always better to serve it explicitly with the using statement:
using (SqlConnection conn = new SqlConnection("")) { conn.Open();
It is often readable and does not rely on people who understand the internal workings of SqlDataAdapter.Fill , only the using statement and connections.
However, if you know that the connection is closed before the adapter uses it (as in, you just created the connection), and it is not used for anything else, your code is completely safe and valid.
Personally, I would write something like this:
string cnStr = "Data Source=TEST;Initial Catalog=Suite;Persist Security Info=True;User ID=app;Password=Immmmmm"; DataSet ds = new DataSet(); using (SqlConnection cn = new SqlConnection(cnStr)) using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Date", cn)) using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { conn.Open(); adapter.Fill(ds); }
Adam houldsworth
source share