我的连接没有显示打开,那我还用关闭连接,释放连接资源吗?
比如我的代码实现方式是这样的:
SqlCommandBuilder cmdDept = new SqlCommandBuilder(daDept);
Con = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
daDept = new SqlDataAdapter("select id,name,descn from CodeDept",SQLHelper.CONN_STRING_NON_DTC);
dsDept = new DataSet(); daDept.Fill(dsDept);
DataGrid1.DataSource = dsDept;
if(!Page.IsPostBack)
{
AccountInfo myAccount = new AccountInfo();
AccountController ac = new AccountController();
myAccount = ac.GetAccountInfo(true);
DataGrid1.DataBind();
Session["index"] = null;
}
这里的Con没有显示地open(),我还需要用Close()和Dispose()方法释放连接吗?因为我现在出现了
"超时时间已到。在从池中获取连接之前超时时间已过。出现这种情况可能是因为所有池连接都已被使用并已达到最大池大小。"这样的错误!谢谢指点!
比如我的代码实现方式是这样的:
SqlCommandBuilder cmdDept = new SqlCommandBuilder(daDept);
Con = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
daDept = new SqlDataAdapter("select id,name,descn from CodeDept",SQLHelper.CONN_STRING_NON_DTC);
dsDept = new DataSet(); daDept.Fill(dsDept);
DataGrid1.DataSource = dsDept;
if(!Page.IsPostBack)
{
AccountInfo myAccount = new AccountInfo();
AccountController ac = new AccountController();
myAccount = ac.GetAccountInfo(true);
DataGrid1.DataBind();
Session["index"] = null;
}
这里的Con没有显示地open(),我还需要用Close()和Dispose()方法释放连接吗?因为我现在出现了
"超时时间已到。在从池中获取连接之前超时时间已过。出现这种情况可能是因为所有池连接都已被使用并已达到最大池大小。"这样的错误!谢谢指点!
{
}
catch()
{
}
finally
{
Conn.Close();
}
if(Conn.State != ConnectionState.Open) Conn.Close();
对你只有好处没有坏处啊!
不过对于SqlDataAdapter,不需要干预的。
对SqlCommand之类的,就需要了。
mazekui(悠悠春竹) 是不是笔误了,应该是
if(Conn.State != ConnectionState.Closed) Conn.Close();