连接未关闭。连接的当前状态为已打开。

taylermail 2013-05-06 11:42:45
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ysconn con1 = new ysconn();
con1.sqlString = "select * from ys_info where info_id='" + Request.QueryString["info_id"] + "'";
SqlDataReader rs = con1.recordset();
int m, j;
string A;
string[] tmpA = new string[1];
if (rs.HasRows)
{
rs.Read();
info_id.Text = rs["info_id"].ToString();

}
rs.Close();
con1.Close();
con1.sqlString = "select x_yslx from ys_xq where info_id='" + Request.QueryString["info_id"] + "' group by x_yslx";
rs = con1.recordset();
while (rs.Read())
{
for (m = 0; m < this.yslx.Items.Count; m++)
{
if (rs[0].ToString() == this.yslx.Items[m].Value)
{
this.yslx.Items[m].Selected = true;
this.yslx.Items[m].Enabled = false;
}
}

}
rs.Close();
con1.Close();
con1.CloseConn();
}
else
{
int m;

for (m = 0; m < this.yslx.Items.Count; m++)
{
if (this.yslx.Items[m].Selected == true)
{

ysconn con1 = new ysconn();
con1.sqlString = "select x_yslx from ys_xq where info_id='" + Request.QueryString["info_id"] + "' group by x_yslx";
SqlDataReader rs = con1.recordset();
rs = con1.recordset();
while (rs.Read())
{
}
rs.Dispose();
con1.CloseConn();

}

}

}
}

始终提示 ysconn.cs 行: 53 错误: 连接未关闭。连接的当前状态为已打开。


public SqlDataReader recordset()
{
SqlCommand mycom = new SqlCommand(_sqlString, mycon);
mycon.Open(); //53行
SqlDataReader reader = mycom.ExecuteReader();
return reader;

}

...全文
85 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
怎么全都混在一起写嘛。。
  • 打赏
  • 举报
回复
引用 1 楼 liuchaolin 的回复:
好乱的代码啊 using(SqlDataReader rs = con1.recordset(CommandBehavior.CloseConnection);) { int m, j; string A; string[] tmpA = new string[1]; if (rs.HasRows) { rs.Read(); info_id.Text = rs["info_id"].ToString(); } //rs.Close(); // con1.Close(); .... }
你后辈们看到你代码准喷你
爱在夕阳下 2013-05-08
  • 打赏
  • 举报
回复
using() 是个不错的办法 写个sqlhelper 比较好
Trust-Me 2013-05-08
  • 打赏
  • 举报
回复
用using(),可以解决你忘掉关连接的问题。推荐百度学一下,基本上大家都在用。
  • 打赏
  • 举报
回复
SqlDataReader 数据查询期间是不能Close的,一般的做法是读取后放在DataTable或者List集合变量里,然后返回
tudiy 2013-05-06
  • 打赏
  • 举报
回复
53行注释掉
md5e 2013-05-06
  • 打赏
  • 举报
回复
好乱的代码啊 using(SqlDataReader rs = con1.recordset(CommandBehavior.CloseConnection);) { int m, j; string A; string[] tmpA = new string[1]; if (rs.HasRows) { rs.Read(); info_id.Text = rs["info_id"].ToString(); } //rs.Close(); // con1.Close(); .... }

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧