: System.InvalidOperationException: 已有打开的与此连接相关联的 DataReader,必须首先将它关闭。

qxkls 2008-12-12 10:39:16
sqlConn = new SqlConnection(BP.DataAccess.Constants.ConnectString); string sql = "select count(name)as c,name from get_movie where datediff(dd,datet,getdate()) <7 and serialno = '"+name+"' group by name";
string sql1 = "insert into get_movie values('"+name+"','"+this.TextBox1.Text+"','"+DateTime.Now.ToString()+"','"+url+"','"+0+"','"+des+"')";
string sql2 = "select counts from hardinfo where serianlo = '"+name+"'";
cmd = new SqlCommand(sql,sqlConn);
cmd1 = new SqlCommand(sql1,sqlConn);
cmd2 = new SqlCommand(sql2,sqlConn);
SqlDataReader read = null;
SqlDataReader read2 = null;
sqlConn.Open();
read = cmd.ExecuteReader();
read2 = cmd2.ExecuteReader();
while(read.Read() && read2.Read())
{

int count = Convert.ToInt32(Convert.ToString(read.GetValue(0)));
int hard_info_count = Convert.ToInt32(read2.GetValue(0));
string names = Convert.ToString(read.GetValue(1));
if(count>hard_info_count)
{

Response.Write("<script>alert('请求失败,一周之内只能请求'"+hard_info_count+"'部影片!')</script>");
p =true;
return;
}
if(names == this.TextBox1.Text)
{
Response.Write("<script>alert('您已请求过此影片,请不要重复求片!')</script>");
p =true;
return;
}
}
sqlConn.Close();
if(p == false)
{
sqlConn.Open();
cmd1.ExecuteNonQuery();
sqlConn.Close();
Response.Write("<script>alert('影片信息已发出请等候!')</script>");
}






--------------


行 73: sqlConn.Open();
行 74: read = cmd.ExecuteReader();
行 75: read2 = cmd2.ExecuteReader();
行 76: while(read.Read() && read2.Read())
行 77: {

...全文
450 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
criedshy 2008-12-12
  • 打赏
  • 举报
回复
没有关闭DataReader
webdiyer 2008-12-12
  • 打赏
  • 举报
回复
执行insert语句应该用ExecuteNonQuery()方法,而计算总记录数应该用ExecuteScalar()方法。
HDNGO 2008-12-12
  • 打赏
  • 举报
回复
如果只是为了得到一个值做判断,用变量吧~

而且,count的话,一定会有一行的,不会为空的~

DataReader用完一定要关闭,要不然过一会儿数据库就挂了~
anncesky 2008-12-12
  • 打赏
  • 举报
回复
你实在是个初学者,还是不要用DataReader吧

用SqlDataAdapter,就这会有这样的问题

DataReader是独占的资源,两个一起打开是一行嘀,用完还要马上关闭
qxkls 2008-12-12
  • 打赏
  • 举报
回复
我While(read.Read())
可以用
我While(read.Read()&&read2.Read())
就提示read2 = cmd2.ExecuteReader();出错了
qinhl99 2008-12-12
  • 打赏
  • 举报
回复
行 73: sqlConn.Open();
行 74: read = cmd.ExecuteReader();
行 75: read2 = cmd2.ExecuteReader();
行 76: while(read.Read() && read2.Read())
行 77: {

//哥们,SqlDataReader的实例对数据库连接是独占的,除非read关闭了,才能把sqlConn给read2用!
troy-zhou 2008-12-12
  • 打赏
  • 举报
回复
因为DataReader是只读的,所以必顶先把它关闭才能执行其他的操作!!!!!!!!
qinhl99 2008-12-12
  • 打赏
  • 举报
回复
只有把read和read2先关闭,才可以再使用与它关联的sqlConn!
HDNGO 2008-12-12
  • 打赏
  • 举报
回复
insert为啥用ExecuteReader?
HDNGO 2008-12-12
  • 打赏
  • 举报
回复
用sqlhelper吧。。。。
lovehongyun 2008-12-12
  • 打赏
  • 举报
回复
错误报的很明显.

要不你就分别指定连接或是把reader换成datatable
jiang_jiajia10 2008-12-12
  • 打赏
  • 举报
回复
下午有活动就不给你整理了。自己好好整理一下我给你几个方法你封装到一个类里面。用的时候调用

//这个方法适合增删改。
public Boolean Exect(string sQueryString) {

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
conn.Open();
SqlCommand cmd = new SqlCommand(sQueryString, conn);
try
{

cmd.ExecuteNonQuery();
conn.Close();
}
catch (System.Exception e) {

conn.Close();
return false;
}
return true;
}
//这个方法适合查
public System.Data.DataSet GetDataSet(string sQueryString, string TableName) {

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
conn.Open();
SqlDataAdapter dbAdapter = new SqlDataAdapter(sQueryString, conn);
DataSet ds = new DataSet();
dbAdapter.Fill(ds, TableName);
conn.Close();
return ds;
}
jiang_jiajia10 2008-12-12
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 macker0030 的回复:]
行 73: sqlConn.Open();
行 74: read = cmd.ExecuteReader();
行 75: read2 = cmd2.ExecuteReader();
行 76: while(read.Read() && read2.Read())
行 77: {

两个DataReader不能共用一个打开的Connection的,你需要把read2使用另一个SqlConnection.
而且楼主请记住,使用SqlDataReader后,必须要及时进行关闭,例如:read.Close()

这段代码的质量的确不敢恭维,我也建议楼主起码学习一下sqlhelper,自己搜一…
[/Quote]
传说真的AJ么。LZ整理一下代码吧。
jiang_jiajia10 2008-12-12
  • 打赏
  • 举报
回复
太乱了建议整理一下代码
查询的时候用dataset插入的时候用command对象。好好整理一下这样太乱了。
[/code]
macker0030 2008-12-12
  • 打赏
  • 举报
回复
行 73: sqlConn.Open();
行 74: read = cmd.ExecuteReader();
行 75: read2 = cmd2.ExecuteReader();
行 76: while(read.Read() && read2.Read())
行 77: {

两个DataReader不能共用一个打开的Connection的,你需要把read2使用另一个SqlConnection.
而且楼主请记住,使用SqlDataReader后,必须要及时进行关闭,例如:read.Close()

这段代码的质量的确不敢恭维,我也建议楼主起码学习一下sqlhelper,自己搜一下吧。
wangzhenyue 2008-12-12
  • 打赏
  • 举报
回复
sqlConn = new SqlConnection(BP.DataAccess.Constants.ConnectString); string sql = "select count(name)as c,name from get_movie where datediff(dd,datet,getdate()) <7 and serialno = '"+name+"' group by name";
string sql1 = "insert into get_movie values('"+name+"','"+this.TextBox1.Text+"','"+DateTime.Now.ToString()+"','"+url+"','"+0+"','"+des+"')";
string sql2 = "select counts from hardinfo where serianlo = '"+name+"'";
cmd = new SqlCommand(sql,sqlConn);
cmd1 = new SqlCommand(sql1,sqlConn);
cmd2 = new SqlCommand(sql2,sqlConn);
SqlDataReader read = null;
SqlDataReader read2 = null;
sqlConn.Open();
read = cmd.ExecuteReader();
read2 = cmd2.ExecuteReader();
while(read.Read() && read2.Read())
{

int count = Convert.ToInt32(Convert.ToString(read.GetValue(0)));
int hard_info_count = Convert.ToInt32(read2.GetValue(0));
string names = Convert.ToString(read.GetValue(1));




//这里啊

read.Close();
read2.Close();




if(count>hard_info_count)
{

Response.Write(" <script>alert('请求失败,一周之内只能请求'"+hard_info_count+"'部影片!') </script>");
p =true;
return;
}
if(names == this.TextBox1.Text)
{
Response.Write(" <script>alert('您已请求过此影片,请不要重复求片!') </script>");
p =true;
return;
}
}
sqlConn.Close();
if(p == false)
{
sqlConn.Open();
cmd1.ExecuteNonQuery();
sqlConn.Close();
Response.Write(" <script>alert('影片信息已发出请等候!') </script>");
}
风骑士之怒 2008-12-12
  • 打赏
  • 举报
回复
MSDN:
在使用 SqlDataReader 时,关联的 SqlConnection 正忙于为 SqlDataReader 服务,对 SqlConnection 无法执行任何其他操作,只能将其关闭。除非调用 SqlDataReader 的 Close 方法,否则会一直处于此状态。例如,在调用 Close 之前,无法检索输出参数。
Adechen 2008-12-12
  • 打赏
  • 举报
回复
应尽量早关闭连接,

62,046

社区成员

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

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

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

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