问题描述
SqlConnectioncon1=newSqlConnection("server=.;database=jsj;uid=sa;pwd=;");con1.Open();intBookid=Convert.ToInt32(this.GVbook.Rows[e.NewSelectedIndex].Cells[0].Text);stringstr1="select*fromBookinfowhereAllbookid="+Bookid;SqlCommandcmd1=newSqlCommand(str1,con1);SqlDataReaderdr1=cmd1.ExecuteReader();if(dr1.Read()){Session["Allbookid"]=dr1["Allbookid"].ToString();Session["Title"]=dr1["Title"].ToString();Session["DBname"]=dr1["DBname"].ToString();}dr1.Close();con1.Close();SqlConnectioncon2=newSqlConnection("server=.;database=jsj;uid=sa;pwd=;");con2.Open();stringstr2="select*fromUpdateinfo";SqlCommandcmd2=newSqlCommand(str2,con2);SqlDataReaderdr2=cmd2.ExecuteReader();if(dr2.Read()){Session["Ordertime"]=dr2["Ordertime"].ToString();}dr2.Close();con2.Close();SqlConnectioncon3=newSqlConnection("server=.;database=jsj;uid=sa;pwd=;");con3.Open();stringtitle=Session["Title"].ToString();stringbookid=Session["Allbookid"].ToString();stringdbname=Session["DBname"].ToString();DateTimetoday=System.DateTime.Now.Date;intordertime=Convert.ToInt32(Session["Ordertime"]);DateTimeshborrow=today.AddDays(ordertime);//定义限借日期stringStrr="select*fromOrderswhereAllbookid="+Bookid;SqlCommandCmdd=newSqlCommand(Strr,con3);SqlDataReaderDrr=Cmdd.ExecuteReader();if(Drr.Read()) //判断预约表中是否有此记录,若无则插入{Session["Allbookid"]=Drr["Allbookid"].ToString();}else{stringstr4="insertintoOrders(Userid,Allbookid,Title,DBname,Ordertime,Shborrow,Isorder)values('"+Labuser.Text+"','"+bookid+"','"+title+"','"+dbname+"','"+today+"','"+shborrow+"','"+0+"')";SqlCommandcmd4=newSqlCommand(str4,con3);cmd4.ExecuteNonQuery();Session["Allbookid"]=Drr["Allbookid"].ToString();}Drr.Close();con3.Close();stringorderbookid=Session["Allbookid"].ToString();Labbookid.Text=orderbookid;Session["aa"]=Labbookid.Text.ToString();Response.Redirect("Orderbook.aspx");总是提示已有打开的与此命令相关联的DataReader,必须首先将它关闭请各位帮忙找一下原因谢谢!
解决方案
解决方案二:
-_-!好乱的代码。if(Drr.Read()) //判断预约表中是否有此记录,若无则插入{Session["Allbookid"]=Drr["Allbookid"].ToString();}else{stringstr4="insertintoOrders(Userid,Allbookid,Title,DBname,Ordertime,Shborrow,Isorder)values('"+Labuser.Text+"','"+bookid+"','"+title+"','"+dbname+"','"+today+"','"+shborrow+"','"+0+"')";SqlCommandcmd4=newSqlCommand(str4,con3);cmd4.ExecuteNonQuery();Session["Allbookid"]=Drr["Allbookid"].ToString();}
用DataReader的时候不能同时在别的地方写代码操作数据库SqlCommandcmd4=newSqlCommand(str4,con3);cmd4.ExecuteNonQuery();
解决方案三:
DataReader使用是独占一个数据库连接的,如果要在DataReader关闭前操作数据库,那就新创建一个SqlConnection
解决方案四:
呵呵见笑了以后多注意!
解决方案五:
你的代码风格太乱了..
解决方案六:
有必要连三次吗,好乱