我有这个代码
DataTable dt= new DataTable();
SqlDataAdapter da;
private void LoadData()
{
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString))
{
da = new SqlDataAdapter("Select * from table",cnn);
da.Fill(dt);
}
}连接会在之后关闭,对吗?如果我想更新DataTable,我怎样才能将da重新连接到cnn?
发布于 2012-08-24 12:55:20
是的,using语句将在SqlConnection上调用Dispose。参见using Statement。
要以这种方式重新连接,您需要使用另一个using语句在原始的using语句中执行更新或更新。
发布于 2012-08-24 12:56:52
Fill方法使用关联的SelectCommand属性指定的SELECT语句从数据源中检索行。与SELECT语句关联的连接对象必须有效,但不必打开。如果在调用Fill之前关闭连接,则会打开连接以检索数据,然后关闭连接。如果在调用Fill之前连接处于打开状态,它将保持打开状态。
来源:See this
https://stackoverflow.com/questions/12103302
复制相似问题