sqlconnection.close

Opening SqlConnection that has been closed already

落爺英雄遲暮 提交于 2019-12-10 15:46:22
问题 Are there any problems with closing and opening of the same SqlConnection object instead of creating new one each time? For example: SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); //Some work here conn.Close() //Some work here... conn stays in scope conn.Open() Is there any chance to get illegal state exception by opening connection second time? 回答1: You can reopen the connection after closing it (you cannot reopen a disposed connection, though). As for Dave Zych's

Does SqlDataAdapter close the SqlConnection after Fill() function?

眉间皱痕 提交于 2019-12-10 02:58:52
问题 Does SqlDataAdapter close the SqlConnection after the Fill() function or do I need close it myself? 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