日期:2014-05-18  浏览次数:20456 次

请教“连接已经打开(state=Open)”问题?
下面一段程序,那里还没有关闭啊?
SqlCommand   pcmd   =   new   SqlCommand();

//遍历FORM表单
for(int   ii=0;ii <Request.Form.Count;ii++)
{

if(Request.Form[ii].ToString().Trim()!= " "   &&   Xh_Class.IsNumeric(Request.Form[ii].ToString().Trim()))
{
pcmd.CommandText   =   "update   tb_votequestion   set   ps=ps+1   where   id= "+Request.Form[ii].ToString().Trim();
pcmd.Connection   =   MyConn;
pcmd.Connection.Open();
pcmd.ExecuteNonQuery();
pcmd.CommandText   =   "update   tb_votetitle   set   vcount=vcount+1   where   id= "+Request.Form.Keys[ii].ToString();
pcmd.ExecuteNonQuery();
pcmd.Dispose();
MyConn.Close();
}
else   if(Request.Form[ii].ToString().Trim().IndexOf( ', ')!=-1)
{
string   strNum=Request.Form[ii].ToString().Trim();
string   strId=Request.Form.Keys[ii].ToString();
string   []   sz=strNum.Split( ', ');
for(int   jj=0;jj <sz.Length;jj++)
{
pcmd.CommandText   =   "update   tb_votequestion   set   ps=ps+1   where   id= "+sz[jj];
pcmd.Connection   =   MyConn;
pcmd.Connection.Open();
pcmd.ExecuteNonQuery();
}
pcmd.CommandText   =   "update   tb_votetitle   set   vcount=vcount+1   where   id= "+strId;
pcmd.ExecuteNonQuery();
pcmd.Dispose();
MyConn.Close();
}

}
pcmd.Dispose();
MyConn.Close();

------解决方案--------------------
沙发,帮忙顶
------解决方案--------------------
pcmd.Connection.Open();
pcmd.ExecuteNonQuery();
con.close();
con.open();
pcmd.CommandText = "update tb_votetitle set vcount=vcount+1 where id= "+Request.Form.Keys[ii].ToString();
pcmd.ExecuteNonQuery();
------解决方案--------------------
一次 pcmd.Connection.Open(); 和MyConn.Close(); 就可以了,把这两个个提到循环外面去
------解决方案--------------------
还不如写个USING 就不用关闭了
------解决方案--------------------
关闭了啊

而且关了好几次。。。。。
------解决方案--------------------
for(int jj=0;jj <sz.Length;jj++)
{
pcmd.CommandText = "update tb_votequestion set ps=ps+1 where id= "+sz[jj];
pcmd.Connection = MyConn;
pcmd.Connection.Open();
pcmd.ExecuteNonQuery();
}
---------建议不要使用这样的循环,多次访问数据库很影响dbserver的。
你可以考虑拼接sql语句,然后再执行
------解决方案--------------------
pcmd.CommandText = "update tb_votequestion set ps=ps+1 where id= "+Request.Form[ii].ToString().Trim();
pcmd.Connection = MyConn;
pcmd.Connection.Open();
pcmd.ExecuteNonQuery();
pcmd.Connection.Close();
MyConn.Open(); //加上这两句试一下
pcmd.CommandText = "update tb_votetitle set vcount=vcount+1 where id= "+Request.Form.Keys[ii].ToString();
pcmd.ExecuteNonQuery();
pcmd.Dispose();
MyConn.Close();