62,046
社区成员
发帖
与我相关
我的任务
分享
public static DataSet GetDataSet(string stringSql)
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(stringSql, conn);
adapter.Fill(ds);
}
return ds;
}
public static int ExecuteScalar(string stringSql)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(stringSql, conn))
{
try
{
conn.Open();
object obj = cmd.ExecuteScalar();
int i = Convert.ToInt32(obj);
return i;
}
catch (System.Data.SqlClient.SqlException ex)
{
conn.Close();
throw ex;
}
}
}
}
string connString = @"Data Source=. 省略若干字符;";
string sql = "select * from Manage_Table";
SqlConnection cn = new SqlConnection(connString);
SqlCommand cm = new SqlCommand(sql,cn);
SqlDataAdapter da = new SqlDataAdapter(cm);
DataTable dt = new DataTable();
da.Fill(dt);
Response.Write(dt.Rows.Count);
Response.Write("<br>");
Response.Write(cn.State );
运行结果是
44
Closed
到数据库里取了数据,一共有44条,然后状态是 close。
请问我的这段代码有啥问题没?(不考虑资源有没有及时释放)
如果没啥问题的话,那么请问,是啥时候close的呢?