using (SqlConnection cn = new SqlConnection(connectionString)) { using (SqlCommand cm = new SqlCommand(commandString, cn)) { cn.Open(); cm.ExecuteNonQuery(); } }There are 2 reasons for this. 1) Using implicitly generates a try/catch around the code. 2) Sqlcommand and SqlConnection use some unmanaged resources, so you must ensure to dispose of the objects when you're done, otherwise you could get a memory leak. "Using" calls dispose for you.
>DataSet1 ds = new DataSet1(); >MessageBox.Show("Coulumns.Count: " + ds.Table2.Columns.Count.ToString(), "before"); > >SqlDataAdapter adapter = new SqlDataAdapter(); >adapter.SelectCommand = new SqlCommand("SELECT field1, field2, field3 FROM Table2", new SqlConnection("Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;")); >adapter.Fill(ds, ds.Table2.TableName); > >MessageBox.Show("Coulumns.Count: " + ds.Table2.Columns.Count.ToString(), "after"); >>