已有打开的与此connand相关联的Datareader,必须首先将他关闭 求回复,属实有点奇怪 [问题点数:20分]

Bbs1
本版专家分:0
结帖率 0%
已有打开与此 Command 相关联的 DataReader,必须首先将它关闭
在运用Linq to sql 或者 linq to entity等相关linq技术进行数据库访问操作时,如果发生上述异常是因为是因为.NET內部是使用DataReader作数据存取,DataReader用同一个Connection。 解决办法有两个: 1,设定ConnectionString加上MultipleActiveResultSets=true,但只适用于SQL 2005以后的
实体类中有外关键字关联另一张表导致查询出现已有打开与此 Command 相关联的 DataReader,必须首先将它关闭的解决
今天在做一个返回json方法时出现了<em>已有</em><em>打开</em>的<em>与此</em> Command <em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>的错误。代码如下: public JsonResult GetCompletedSimulation()         {             var service = new ExamMainService();             var list = ser
已有打开与此命令相关联的 DataReader,必须首先将它关闭。"问题.
上周在项目开发中,遇到这样的一个问题,在前台展示页,两个WebPart调用后台的API进行数据的查询呈现。有时候两个数据报表都能呈现,而有时候,却只能呈现一个报表。使用IE的开发人员调试工具跟踪调试以后,发现了如上提到的问题“<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>”。 起先以为是API中使用了DataReader进行数据读取,没有<em>关闭</em>,检查了一下代码,发现代码中并没有
错误:“已有打开与此命令相关联的 DataReader,必须首先将它关闭”的解决方法。
主要原因是 DataReader 在<em>打开</em>使用时,一个 SqlConnection 只允许<em>打开</em>一个 DataReader,在开启另一个 DataReader 时,<em>必须</em>将前面一个先 Close 掉。在使用快照隔离实例(一)中就出现过这样的问题,错误实在 transaction2.Commit();  中,我们在只要在其前面加上语句: reader2.Close(); 即可解决错我。
c#已有打开与此COMMAND相关联的DataReader
string connect = null;   string sql = null; SqlDataReader qlddr = null;  SqlConnection S_con = null; DateTime DT = System.DateTime.Now;  string dt = System.DateTime.Now.ToString();  connect = "Server=
已有打开与此命令相关联的 DataReader,必须首先将它关闭"">解决方案之"已有打开与此命令相关联的 DataReader,必须首先将它关闭"
        在做网站的过程中出现如题的错误,自己检查了代码发现并没有使用DataReader,怎么会出现为<em>关闭</em>了。于是百度了下,说ExecuteNonQuery()生成的对象是<em>datareader</em>,于是把所以该关的都关掉,包括Connection,结果还是这样。继续百度。结果新的发现,Connnection对象不能用一个全局的静态对象,于是去掉static后,运行就没哟问题。困扰了几天的问题终于解决。 
如何解决“已有打开与此命令相关联的 DataReader,必须首先将它关闭。”
标题中的异常相信比较多的朋友都遇到过,今天刚好遇到这个问题,也得到较全的解决方案,简单归纳如下:1、主要原因是 DataReader 在<em>打开</em>使用时,一个 SqlConnection 只允许<em>打开</em>一个 DataReader,在开启另一个 DataReader 时,<em>必须</em>将前面一个先 Close 掉。 2、或许有朋友会问,我在语句中并未使用 DataReader,未何也提示同样的错误,这个Data
已有打开与此Command相关联的DataReader必须首先将他关闭
using System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Data.SqlClient;rnusing System.Drawing;rnusing System.Linq;rnusing System.Text;rnusing System.Windows.Forms;rnrnnamespace LOADrnrn public partial class Form1 : Formrn rn public Form1()rn rn InitializeComponent();rn rnrn private void button2_Click(object sender, EventArgs e)rn rn rn Form2 fm2 = new Form2();rn fm2.ShowDialog();rn rn rn private void button1_Click(object sender, EventArgs e)rn rn rn if (textBox1.Text == "")rn MessageBox.Show("请输入用户名!");rn else if (textBox2.Text == "")rn MessageBox.Show("请输入密码!");rn elsern rn string con = "Data source=DESKTOP-A0GNH22\\SCW;Initial Catalog=用户登录;integrated Security=true;";rn SqlConnection sqlcon = new SqlConnection(con);rn sqlcon.Open();rn string str1="select id from 用户表 where id=('"+textBox1.Text+"')";rn string str2="select 密码 from 用户表 where 密码=('"+textBox2.Text+"')";rn string str3="select 错误次数 from 用户表 where id=('"+textBox1.Text+"')";rn SqlCommand sqlcmd1 = new SqlCommand(str1, sqlcon);rn SqlCommand sqlcmd2=new SqlCommand(str2,sqlcon);rn SqlCommand sqlcmd3=new SqlCommand(str3,sqlcon); rn SqlDataReader reader1 = sqlcmd1.ExecuteReader();rn SqlDataReader reader2=sqlcmd2.ExecuteReader();rn SqlDataReader reader3=sqlcmd3.ExecuteReader();rn reader1.Read();rn reader2.Read();rn reader3.Read();rn string result1 = reader1["id"].ToString();rn string result2 = reader2["密码"].ToString();rn string result3 = reader3["错误次数"].ToString();rn int s=Convert.ToInt32(result3);rn if (s == 3)rn rn button2.Enabled = false;rn sqlcon.Close();rn rnrn int t = 3 - s;rn for (s = 1; s );rn sqlcon.Close();rn this.Hide();rn Form3 fm3 = new Form3();rn fm3.Show();rn rn rn rn rn rn rn rn rnrnrnrnrn新手第一次做这个 ,不知道该在哪里<em>关闭</em>那个东西...求教rn
已有打开与此命令相关联的 DataReader,必须首先将它关闭
请问我这个怎么解决啊?rnrn下面是我的代码rnstring sqlstr = "select [Tip],[Tdate] from tongji where [Tip]='" + serverip + "' and [Tdd]='" + dd + "'";rn System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlstr, con);rn tryrn rn con.Open();rn System.Data.SqlClient.SqlDataReader reader;rn reader = cmd.ExecuteReader();rn if (reader.Read())rn rnrn rn elsern rn System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand("insert into tongji (Tip,Tdate,Thh,Tmm,Tadd,Tdd,TY) values (@Tip,@Tdate,@Thh,@Tmm,@Tadd,@Tdd,@TY)", con);rn cmd2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Tip", System.Data.SqlDbType.VarChar, 20));rn cmd2.Parameters["@Tip"].Value = serverip;rn cmd2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Tdate", System.Data.SqlDbType.VarChar, 20));rn cmd2.Parameters["@Tdate"].Value = time;rn cmd2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Thh", System.Data.SqlDbType.VarChar, 20));rn cmd2.Parameters["@Thh"].Value = hh;rn cmd2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Tmm", System.Data.SqlDbType.VarChar, 20));rn cmd2.Parameters["@Tmm"].Value = mm;rn cmd2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Tadd", System.Data.SqlDbType.VarChar, 100));rn cmd2.Parameters["@Tadd"].Value = add;rn cmd2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Tdd", System.Data.SqlDbType.VarChar, 100));rn cmd2.Parameters["@Tdd"].Value = dd;rn cmd2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@TY", System.Data.SqlDbType.VarChar, 100));rn cmd2.Parameters["@TY"].Value = yy;rn cmd2.ExecuteNonQuery();rn reader.Close();rn rn rn catch (Exception ex)rn rn throw (ex);rn rn finallyrn rn con.Close();rn
已有打开与此命令相关联的 DataReader,必须首先将它关闭???
//情况是这样的,我先访问数据库,一边read一边判断,如果read到数据跟我输入关键数据相等,就进行更新,这个时候用于是直接在read里面更新,会报错“<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>”,但这个时候又不能关掉read,怎么解决呢?rn
已有打开与此命令相关联datareader必须首先将它关闭
内容是 protected void btnSubmit_Click(object sender, ImageClickEventArgs e)rn rn rn SqlConnection con = db.GetConnection();rn con.Open();rn for (int i = 0;i< lstSelect.Items.Count;i++ )rn rn string str = "select Sno from Student where Name='" + this.lstSelect.Items[i].ToString() + "'";rn SqlCommand com = new SqlCommand(str,con);rn SqlDataReader sdr = com.ExecuteReader();rn rn while (sdr.Read())rn rn string Sno = sdr["Sno"].ToString();rn string sql = "insert into Outside(NewsID,Sno) values(ID,Sno)";rn com.CommandText = sql;rn com.ExecuteNonQuery();rn sdr.Close();rnrn con.Close();rnrn rn cc.MessageBox("提交成功!","Mon_OutAttend.aspx");rnrn rn
已有打开与此连接相关联的 DataReader,必须首先将它关闭
如果把用rs.Close();上面的错误就会消失 但循环后就会出现DataReader已<em>关闭</em>rn请问我这个rs.Close();加在哪? 或者我怎么弄这个循环rn高手顺便帮我看看代码有没有错误 我第一次用DataTable绑定DataGridrnrnrnprivate void Page_Load(object sender, System.EventArgs e)rn rn if(!Page.IsPostBack)rn rn DataTable table=new DataTable();rn table.Columns.Add("ID");rn table.Columns.Add("类别名称");rn table.Columns.Add("所属类别");rn SqlConnection conn=ManageFunction.Create();rn conn.Open();rn SqlCommand cmd=new SqlCommand("select * from pang_book_leibie where leibieid=0",conn);rn SqlDataReader rs=cmd.ExecuteReader();rn while(rs.Read())rn rn DataRow dr=table.NewRow();rn dr["ID"]=rs["id"].ToString();rn dr["类别名称"]=rs["bookname"].ToString();rn dr["所属类别"]="大类";rn string leibieid=rs["leibieid"].ToString();rn //rs.Close();rn SqlCommand cmd2=new SqlCommand("select * from pang_book_leibie where leibieid="+leibieid,conn);rn SqlDataReader rs2=cmd2.ExecuteReader();rn table.Rows.Add(dr);rn rnrn while(rs2.Read())rn rn dr=table.NewRow();rn dr["ID"]=rs2["id"].ToString();rn dr["类别名称"]=rs2["bookname"].ToString();rn dr["所属类别"]="小类";rn table.Rows.Add(dr);rn rn rn rs2.Close();rn cmd2.Dispose();rn rn rn this.MyDataGrid.DataSource=table;rn this.MyDataGrid.DataBind();rn rnrn rnrn
已有打开与此连接相关联的DataReader,必须首先将它关闭
''dbAccess class 中定義 rnPublic Function GetDTableBySql(ByVal strSQL As String, Optional ByVal sqlParam() As SqlParameter = Nothing) As Booleanrn Tryrn Dim sqlCmd As SqlClient.SqlCommand = New SqlClient.SqlCommandrnrn With sqlCmdrn If BoolTranc = True Then .Transaction = Trancrn .Connection = Connectionrn .CommandText = strSQLrn '添加一些参数rn If Not sqlParam Is Nothing Thenrn .Parameters.AddRange(sqlParam)rn End Ifrn End Withrn Dim sqlAdp As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sqlCmd)rn Dim dt As DataTable = New DataTablern sqlAdp.Fill(dt)rn sqlAdp = Nothingrn sqlCmd = Nothingrn Me.ResultTable = dtrn Return TruernCatch ex As Exceptionrn rn ErrorMessage = ex.Messagern Return FalsernEnd Tryrnrn End Functionrn------------------------------------rn''進行調用rn dbAccess.OpenConnection()rn '查询sqlrn resultDBAccess = dbAccess.GetDTableBySql(sqlQuery.ToString)rn If resultDBAccess = True Thenrn param.SelectDataTable = dbAccess.ResultTablern End Ifrnrn dbAccess.CloseConnection()rnrn説明:rn這樣的調用有很多地方,一個客戶端的時候沒發現錯誤。當 多個客戶端時有時候會出現標題的錯誤。rn
已有打开与此命令相关联的 DataReader,必须首先将它关闭。??
错误提示:<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnrn public void JoinKf()rn rn int id = Convert.ToInt32(Request.QueryString["id"]);rn //读取数据库连接字符串rn string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);rn //创建数据库连接rn SqlConnection myconn = new SqlConnection(settings);rn myconn.Open();rn //创建查询语句rn string sql = "select * from Student_xx As a Left Join Student_xj As b on a.StuId = b.StuId Where a.StuId=" + id;rn //创建数据库命令rn SqlCommand mycmd = new SqlCommand(sql, myconn);rn SqlDataReader result = mycmd.ExecuteReader();rn if (result.Read())rn rn string pczt = "未赔偿";rn string sql1 = "insert into KouFei(StuId, Xh, StuName, Sex, Xs, Zy, Nj, ClassName, KfJe, KfYy, KfR, KfBm, KfTime, PcZt) Values(" + id + "," + "'" + result["Xh"].ToString() + "'," + "'" + result["StuName"].ToString() + "'," + "'" + result["Sex"] + "'," + "'" + result["Xs"].ToString() + "'," + "'" + result["Zy"].ToString() + "'," + "'" + result["Nj"].ToString() + "'," + "'" + result["ClassName"].ToString() + "'," + "'" + tbx_kfje.Text.ToString() + "'," + "'" + tbx_kfyy.Text.ToString() + "'," + "'" + Session["UserName"].ToString() + "'," + "'" + Session["UserBm"].ToString() + "'," + "'" + DateTime.Now + "'," + "'" + pczt + "')";rn //创建数据库命令rn SqlCommand mycmd1 = new SqlCommand(sql1, myconn);rn [color=#FF0000]mycmd1.ExecuteNonQuery();[/color] rn rn myconn.Close();rn
已有打开与此命令相关联的 DataReader,必须首先将它关闭
public class SqlDBrn rn private static SqlConnection connection; //构建连接字符串rn public static SqlConnection Connectionrn rn getrn rn string connectionString = ConfigurationManager.ConnectionStrings["sql"].ToString();//读取配置文件rn if (connection == null)rn rn connection = new SqlConnection(connectionString);rn connection.Open();rn rn else if (connection.State == System.Data.ConnectionState.Closed)rn rn connection.Open();rn rn else if (connection.State == System.Data.ConnectionState.Broken)rn rn connection.Close();rn connection.Open();rn rn return connection;rn rn rn public static DataTable GetDataSet(string safeSql)rn rn DataSet ds = new DataSet();rn SqlCommand cmd = new SqlCommand(safeSql, Connection);rn SqlDataAdapter da = new SqlDataAdapter(cmd);rn da.Fill(ds); //到这里是 报错 [color=#FF0000]“<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em> ”[/color] return ds.Tables[0];rn rn rn //执行(增、删、改)返回受影响的行数rn public static int ExecuteCommand(string safeSql)rn rn SqlCommand cmd = new SqlCommand(safeSql, Connection);rn int result = cmd.ExecuteNonQuery();rn return result;rn rn rnrnweb.config 是这样写的 rn我用的 是 SqlServer 2000 vs2005 我所有 的查询都用这个方法完成GetDataSet(string safeSql) safeSql是sql语句rn我在同一台电脑上运行 当一个用户在查询大量数据时 后面的人在查询就报“<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。 两个用户都是调用 GetDataSet()方法rn
已有打开与此 Command 相关联的 DataReader,必须首先将它关闭
reader 和 connection 都<em>关闭</em>了,为什么还会出现上面错误呢(如题)rnrn前台:rnrn[code=HTML]rn rn rn rn[/code]rnrn后台:rnrn[code=C#]rnprotected string sConnString = "..."; // 连接数据库字符串rnprotected void Page_Load(object sender, EventArgs e)rnrn menu.InnerHtml = CreateMenuHtml(); rnrnrnpublic string CreateMenuHtml()rnrn StringBuilder sMenuHtml = new StringBuilder();rn if(isPurview("itemA")) sMenuHtml.append(...) ......rn if(isPurview("itemB")) sMenuHtml.append(...) ......rn if(isPurview("itemC")) sMenuHtml.append(...) ......rn if(isPurview("itemD")) sMenuHtml.append(...) ......rn if(GetOther("@ABlock", "ABlock")==1) sMenuHtml.append(...) ...... // 添上了这句就出现如题的错误。 rn return sMenuHtml.ToString();rnrnrn//rnpublic bool isPurview(string item)rnrn SqlConnection connection = new SqlConnection(this.sConnString);rn SqlCommand command = new SqlCommand("IS_Purview", connection);rn command.CommandType = CommandType.StoredProcedure;rn SqlParameter username = command.Parameters.Add("@sUserName", SqlDbType.Char, 50);rn SqlParameter purview = command.Parameters.Add("@sPurview", SqlDbType.Char, 50);rn SqlParameter intReturn = command.Parameters.Add("rv", SqlDbType.Int);rn username.Direction = ParameterDirection.Input;rn purview.Direction = ParameterDirection.Input;rn intReturn.Direction = ParameterDirection.ReturnValue;rn username.Value = Session["username"].ToString();rn purview.Value = item;rn if (connection.State == ConnectionState.Closed)rn rn connection.Open();rn rn SqlDataReader reader = command.ExecuteReader();rn int returnValue = int.Parse(command.Parameters["rv"].Value.ToString());rn reader.Close();rn connection.Close();rn if (returnValue == 1)rn return true;rn elsern return false; rn rnrnpublic int GetOther(string item, string itemvalue)rnrn int iOther=0;rn SqlConnection connection = new SqlConnection(this.sConnString);rn SqlCommand command = new SqlCommand("IS_Other", connection);rn command.CommandType = CommandType.StoredProcedure;rn SqlParameter OtherList = command.Parameters.Add(item, SqlDbType.Char, 50);rn OtherList.Direction = ParameterDirection.Input;rn OtherList.Value = itemvalue;rn if (connection.State == ConnectionState.Closed)rn rn connection.Open();rn rn SqlDataReader reader = command.ExecuteReader();rn reader = command.ExecuteReader();rn if (reader.Read())rn rn iOther = Convert.ToInt32(reader[0]);rn rnrn reader.Close();rn connection.Close();rn return iOther;rnrn[/code]
已有打开与此命令相关联datareader 必须首先将它关闭
登录 提示该错误 但是 有部分人能登上 有人登不上 对方服务器是个老服务器 内存1G 使用数据库 sqlserver2005,我没有使用Datareader 请问这个问题是对方服务器造成的还是 我代码造成的 ,如果是我代码 应该是哪里出错?
.NET : 一定不要忘记关闭DataReader对象
 今天早上处理一个紧急的客户反应问题,这是去年开发的一套绩效考核系统。他们现在集团在推广使用,用户数多起来之后就发现某个页面响应速度非常慢,甚至会报告错误,无法完成操作。 经过检查,发现有一个方法用到了DataReader对象,但当时可能是疏忽了,忘记<em>关闭</em>该对象。特此记录这个问题,以为警示。 /// /// 循环按照编号去取某个员工的某个指标的值 ///
已有打开与此命令相关联的 DataReader,必须首先将它关闭
原因:同时<em>打开</em>建立两个DataReader,而采用同一个SqlConnection(或OleDbConnection );Command(或OleDbCommand )会造成这种结果。解决方法: 两个DataReader用不同的SqlConnection,Command;注意: DataReader依托于SqlConnection,如果SqlConnection<em>关闭</em>了,DataReader
已有打开与此命令相关联的 DataReader,必须首先将它关闭
[code=C#]rn这是我的代码,我在网上找了很久,也知道它错的原因,但我还是不能改好它。还希望帮我优化一下我的代码。谢谢...rnrnrn string cnsql = "server=.;database=wuliu;Integrated Security=SSPI";rn SqlConnection mycon = new SqlConnection(cnsql);rn mycon.Open();rn string str = "select * from dingdan where ID = '" + Session ["danhao"] + "'";rn SqlCommand cmd = new SqlCommand();rn cmd.Connection = mycon;rn cmd.CommandText = "select * from dingdan where ID = '" + Session["danhao"] + "'";rn SqlDataReader dr = cmd.ExecuteReader();rn if (dr.Read())rn rn SqlDataAdapter sda = new SqlDataAdapter(str, mycon);rn DataSet ds = new DataSet();rn sda.Fill(ds, "mytb");rn DataView dv = ds.Tables["mytb"].DefaultView;rn GridView1.DataSource = dv;rn GridView1.DataBind();rn dr.Close();rn rn else rn Response.Write(" ");rn rn mycon.Close();rnrnrn[/code]
已有打开与此命令相关联的 DataReader,必须首先将它关闭
[b][b]DAL层[/b][/b]rnrn public static SqlDataReader GetReader(string safeSql)rn rn SqlDataReader s = null;rnrn using (SqlCommand cmd = new SqlCommand(safeSql, Connection))rn rnrn s = cmd.ExecuteReader(CommandBehavior.CloseConnection);rn rn return s;rn rnrn[b][b]BLL层[/b][/b]rnrn public BookType GetTypeById(int id)rn rn string sql = "select * from BookType where BookTypeId=" + id;rn tryrn rn BookType type = new BookType();rn using (SqlDataReader sqldr = DBHelper.GetReader(sql))rn rn while (sqldr.Read())rn rn type.BookTypeId = Convert.ToInt32(sqldr["BookTypeId"]);rn type.BookTypeNnmber = sqldr["BookTypeNnmber"].ToString();rn type.BookTypeName = sqldr["BookTypeName"].ToString();rn sqldr.Close();rn rn return type;rn rn catch (Exception a)rn rn Console.WriteLine(a.Message);rn throw a;rn rn
已有打开与此命令相关联的 DataReader,必须首先将它关闭……
[code=C#]string sqlstrStudent = "select * from Student where ClassName='" + this.DropDownListCl.SelectedValue .ToString ()+ "'";rn SqlCommand commStudent = new SqlCommand(sqlstrStudent, conn);rn using (SqlDataReader reader = commStudent.ExecuteReader())rn rn while (reader.Read())rn rn string sqlstrStuCous = "insert into Student_Course(StudentID,CourseName,TchID)values('" + reader["StudentID"] + "','" + this.txtName.Text + "','" + Session["UserID"] + "')";rn SqlCommand commStuCous = new SqlCommand(sqlstrStuCous, conn);rn commStuCous.ExecuteNonQuery();rnrn rn [/code]rn为啥还是提示未<em>关闭</em><em>datareader</em>呢。。。求各位大大解决……
已有打开与此命令相关联的 DataReader,必须首先将它关闭
求大神解决一下。已经用reader.Close()了。怎么还错误!rnpublic static tbBook GetBookById(string BId)rn rn string sql = "select * from tb_Book where Bookid =@Bookid";rn using (SqlDataReader reader = connDBHelper.GetReader(sql, new SqlParameter("@Bookid", BId)))rn rn rn tryrn rnrnrn if (reader.Read())rn rn tbBook book = new tbBook();rn book.Bookid = (string)reader["Bookid"];rn book.Bookname = (string)reader["Bookname"];rn book.Bookclass = BookClassService.GetBookClassById((int)reader["Bookclass"]);rn book.Author = (string)reader["author"];rn book.Image = (string)reader["image"];rn book.Publish = (string)reader["publish"];rn book.Banci = (string)reader["banci"];rn book.Price = (double)reader["price"];rn book.Language = (string)reader["language"];rn book.Page = (int)reader["page"];rn book.Number = (int)reader["number"];rn book.Kuc = (int)reader["kuc"];rn book.Sales = (int)reader["sales"];rn reader.Close();rn return book;rn rnrn elsern rn reader.Close();rn return null;rn rnrn rnrn catch (Exception e)rn rn Console.WriteLine(e.Message);rn throw e;rnrn rnrn rn rn
已有打开与此命令相关联的DataReader,必须首先将它关闭
我在asp.net(c#)中,有一个查询功能:rndataset ds=dbhelpersql.getdataset("select * from ...");rnrngetdataset的代码如下:rnpublic static dataset setdataset(string sqlstring)rnrnopen();rnsqldataadapter adapter=new sqldataadapter(sqlstring,conncetion);rndataset dataset=new dataset();rnadapter fill(dataset);rnclose();rnretuern dataset;rnrnrnopen()的代码如下:rnpublic static open()rnrn if(connection==null)rnrnconnection=new sqlconnection(connectionstring);rnrnif (connection.state.equals(connectionstate.closed))rnconnection.open();rnrnrnclose()代码如下:rnpublic static close()rnrnrnif(connection!=null)rnrnconnection.close();rnrnrnrn当我在二台电脑上快速按下同一按钮一段时间,会出现“<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>”,在其他地方也有这个想象,我电脑系统是xp,sql2000rn查了一下网上,说是要<em>关闭</em><em>datareader</em>,不知如何处理,请高手指教,不胜感谢!rn
已有打开与此连接相关联的 DataReader,必须首先将它关闭
<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。 rn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: System.InvalidOperationException: <em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnrn源错误: rnrnrn行 96: cmd.Connection=clsPublic.getConn();rn行 97: cmd.CommandText="Select Top 1 fdUserID From tb_UserInfo where fdUserName='"+strUser+"'";rn行 98: bool blnHasUser=cmd.ExecuteReader().HasRows;rn行 99: cmd.Dispose();rnrn rnrnrnrn我没有<em>打开</em>什么Reader啊???
已有打开与此连接相关联的 DataReader,必须首先将它关闭。"">"已有打开与此连接相关联的 DataReader,必须首先将它关闭。"
没<em>打开</em>任何“显示的”DataReader,却出现这个错误,除了<em>打开</em>多个connection或使用dataset外,还有其它办法吗?rn代码如下:(代码只是测试用而已),请大家帮忙看看rnrnrn class GetTextLiveFilern rn static void Main(string[] args)rn rn conn.Open();rnrn Thread[] thread_GetRemoteFile1=new Thread[100];rnrn ThreadStart getRemoteFileStart1=new ThreadStart(t1);rn thread_GetRemoteFile1[10]=new Thread(getRemoteFileStart1);rnrn getRemoteFileStart1=new ThreadStart(t2);rn thread_GetRemoteFile1[11]=new Thread(getRemoteFileStart1);rn rn thread_GetRemoteFile1[10].Start();rn thread_GetRemoteFile1[11].Start();rnrn rnrn static void t1()rn rn SqlCommand cmd=new SqlCommand();rn cmd.Connection=conn;rnrn cmd.CommandText="insert into TextLiveDetail(liveID,content) values(10,'233233222234444444444444444444444444')";rn for(int i=0;i<1000;i++) cmd.ExecuteNonQuery();rn rnrn
已有打开与此命令相关联的 DataReader,必须首先将它关闭????
<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnSqlCommand com1 = new SqlCommand("select fabric_name,proportion_name,pcs from d_fabric where dele='a'", Con);rn SqlDataReader dr1 = com1.ExecuteReader();rn i =0;rn while (dr1.Read())rn rn app.Cells[3,6+i] = dr1["fabric_name"].ToString().Trim();rn app.Cells[6,7+i] = dr1["proportion_name"].ToString().Trim();rn app.Cells[10,7+i] = dr1["pcs"].ToString().Trim();rn SqlCommand com2 = new SqlCommand("select * from df_cpyc where asd='"+dr1["pcs"].ToString()+"'", Con);rn SqlDataReader dr2 = com2.ExecuteReader(); //<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rn dr2.Read();rn dr2.Close();rn rn dr1.Close();
已有打开与此命令相关联的DataReader,必须首先将它关闭
我是用VB.net做网站的,网站有时候就要报上面那个错误,刷新一次就没问题了。rn Public Function Find(ByVal Sql As String) As DataTablern Dim _Table As New DataTablern Dim _Ad As New Data.OleDb.OleDbDataAdapterrn If _Conn.State = ConnectionState.Closed Then _Conn.Open()rn _Cmd.Connection = _Connrn _Cmd.CommandText = Sqlrn _Ad.SelectCommand = _Cmdrn _Ad.Fill(_Table)rn Find = _Tablern _Conn.Close()rn _Ad = Nothingrnrn End Functionrn这个是我数据库查询。rn我的数据库连接:rn Public Function DbConn(ByVal Sql As String) As SqlClient.SqlConnectionrn Dim strConn As String = "。。。"rn Tryrn DbConn = New Data.SqlClient.SqlConnection(strConn)rn If IsDBNull(DbConn) Thenrn DbConn = New Data.SqlClient.SqlConnection(strConn)rn DbConn.Open()rn ElseIf DbConn.State = System.Data.ConnectionState.Closed Thenrn DbConn.Open()rn ElseIf DbConn.State = System.Data.ConnectionState.Broken Thenrn DbConn.Close()rn DbConn.Open()rn End Ifrn Return DbConn rn Catch ex As Exceptionrn Throw New Exception("创建MsSQl数据库的链接失败!")rn Return Nothingrn End Tryrnrn End Functionrn我在外面调用的时候:Dim dt As Data.DataTable = db.FindSQL("select * from " & tablename & " where " & where)rn直接到Find函数的时候就报上面错误。应该怎么改呢?我看C#里面有using直接释放?VB.NET里面不知道怎么弄了?
(C#)使用已有打开与此命令相关的DataAdapter,必须首先将它关闭”的解决方法
象依赖的Connection不能再用来执行其他Command命令。所以你的代码段会报“<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。” 错误提示。 解决方法如下: 1、reader跟cmOrder两个对象不要共用一个Connection对象。 2、不要在while代码段内执行reader.Close();语句。否则继续执行while代码段内语句会报“阅读器<em>关闭</em>时尝试调用
打开与此连接相关联的 DataReader,必须首先将它关闭
using(SqlConnection conn = new SqlConnection(connectionstring))rn rn SqlDataReader dr=SqlHelper.ExecuteReader(conn,CommandType.Text,"select * from table WHERE F_id='"+User+"'");rn if(dr.Read())rn SqlHelper.ExecuteNonQuery(conn,System.Data.CommandType.Text,"update table2 set F_ID='"+hidNetID+"' where F_bh='"+User+"'");rn elsern SqlHelper.ExecuteNonQuery(conn,System.Data.CommandType.Text,"insert into table2 (F_bh,F_ID) values('"+User+"','"+hidNetID+"')");rnrnrn这段程序总是出错,请问ExecuteNonQuery 跟dr有什么关系?rnrn
使用DataReader和事务时报错: 已有打开与此连接相关联的 DataReader,必须首先将它关闭
异常详细信息: System.InvalidOperationException: <em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnrn while(dr.Read())rn rn show.Text=" customerid is "+dr[0]rn +" ...companyname is "+dr["companyname"]rn +"contactname is >"+dr[dr.GetOrdinal("contactname")];rn rn tra.Commit();rndr.Close();rn rn rn catch(Exception ex)rn rn tra.Rollback(); rn
已有打开与此命令相关联的DataReader
为什么在datatable的ColumnChanged事件中放入cmdMain.ExecuteNonQuery()命令,当保存 daInmovd.Update(dsMain, "inmovd")rn 时会出"<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的DataReader"这错? rn Private Sub dtInmovd_ColumnChanged(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles dtInmovd.ColumnChangedrnrn Dim drInmovh As DataRowrn Dim drInmovd As DataRowrn Dim iTotalCost As Decimalrnrn Tryrnrn If e.Column.ColumnName = "ITEM_NO" And InmovdHasChange Thenrnrn 'MsgBox("select item_no,description,stock_uom,main_loc from maitem where item_no='" + e.Row("ITEM_NO") + "'")rnrn InmovdHasChange = Falsernrn Dim dsMaitem As New DataSetrn cmdMain = New SqlClient.SqlCommand("select item_no,description,stock_uom,main_loc from maitem where item_no='" + e.Row("ITEM_NO") + "'", cnMain)rn cmdMain.CommandType = CommandType.Textrn cmdMain.CommandTimeout = 600rn cmdMain.ExecuteNonQuery()rn cmdMain.Dispose()rn daMain.SelectCommand = cmdMainrn daMain.Fill(dsMaitem, "maitem")rnrn DataGridView1.DataSource = dsMaitemrn DataGridView1.DataMember = "maitem"rnrn If dsMaitem.Tables("maitem").Rows.Count > 0 Thenrn e.Row("DESCRIPTION") = dsMaitem.Tables("maitem").Rows(0)("description").ToStringrn Elsern MsgBox("错误物料编号")rn End Ifrnrn End Ifrn Catch ex As Exceptionrn MessageBox.Show(ex.ToString)rn Finallyrn InmovdHasChange = Truern End Tryrnrn End Sub
已有打开与此连接相关联的DataReader"问题">"已有打开与此连接相关联的DataReader"问题
<em>打开</em>一个数据库中主表(table1)——从表(table2)的程序如下: rnrnOleDbCommand cmd; rnOleDbDataReader dr; rnOleDbCommand cmd2; rnOleDbDataReader dr2; rnrncmd=new OleDbCommand("select * from table1",cn); rndr=cmd.ExecuteReader(); rnwhile(dr.Read()) rn rnrn cmd2=new OleDbCommand("select * from table2 where blockid=" + dr.GetInt32(0) +";",cn); rnrn //程序总是在这里出错,错误信息如下: rn //<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em> rn dr2=cmd2.ExecuteReader(); rnrn while(dr2.Read()) rn rn //代码略... rnrn rn dr2.Close(); rn dr2=null; rn cmd2.Dispose(); rn cmd2=null; rnrn rnrndr.Close(); rndr=null; rncmd.Dispose(); rncmd=null; rncn.Dispose(); rncn=null rnrnrn感觉好象DataReader在同一时间只能<em>打开</em>一个??? rn谁能说说这是怎么回事?谢谢先!
已有打开与此命令相关联的DATAREADER
Dim dr As SqlDataReaderrn stra = "SELECT * from wlxx"rn sqlcmd = New SqlCommand(stra, CONN)rn CONN.Open()rn dr = sqlcmd.ExecuteReaderrn rnrn I = 0rn While dr.Readrn strb = "insert into wlxx1 values('" & dr(0) & "','" & dr(1) & "','" & dr(2) & "','" & dr(3) & "','" & dr(4) & "'"rn sqlcm = New SqlCommand(strb, CONN)rnrn sqlcm.ExecuteNonQuery()rnrn I = I + 1rn Label1.Text = "有" & I & "条数据复制"rn End Whilernrn CONN.Close()rnrn提示“<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的DATAREADER” 错误,是那错误了,晕
System.InvalidOperationException: 已有打开与此命令相关联的 DataReader,必须首先将它关闭
源错误: rnrnrn行 35: SqlDataAdapter da = new SqlDataAdapter(str, conn);rn行 36: DataSet ds = new DataSet();rn行 37: da.Fill(ds, "score");rn行 38: DataGrid1.DataSource = ds;rn行 39: DataGrid1.DataBind();rn rnrn源文件: e:\kechen\tcmg.aspx.cs 行: 37 rnrn[code=C#] rnstring strcon = "data source=www-51f7b2a9835\\sqlexpress;initial catalog=student;integrated security=SSPI";rn SqlConnection conn = new SqlConnection(strcon);rn conn.Open();rn string sel="select * from teacher where t_no='"+TextBox2 .Text .ToString ().Trim ()+"'and t_pas ='"+TextBox1 .Text.ToString ().Trim () +"'";rn SqlCommand cmd = new SqlCommand(sel, conn);rn SqlDataReader read = cmd.ExecuteReader();rn conn.Close();rn if (read.Read())rn rn LinkButton1.Visible = true;rn LinkButton2.Visible = true;rn string str = "SELECT score.c_no, sco.s_no, sco.score FROM score INNER JOIN sco ON score.c_no = sco.c_no";rn SqlDataAdapter da = new SqlDataAdapter(str, conn);rn DataSet ds = new DataSet();rn da.Fill(ds, "score");rn DataGrid1.DataSource = ds;rn DataGrid1.DataBind();rn rn elsern rn Label1.Text = "帐号或密码有错误";rn [/code]rn
异常详细信息: System.InvalidOperationException: 已有打开与此连接相关联的 DataReader,必须首先将它关闭
“/”应用程序中的服务器错误。 rn-------------------------------------------------------------------------------- rnrn<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。 rn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: System.InvalidOperationException: <em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。 rnrn源错误: rnrn执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。 rnrn堆栈跟踪: rnrnrn[InvalidOperationException: <em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。] rn System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing) +292 rn System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +80 rn System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) +45 rn System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +5 rn System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +304 rn System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +77 rn System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +38 rn Wendang.myclass.mydb.S_readDataTable(String strsql) rn Wendang.myclass.OperPubWend.GetWendInfo(String id) rn Wendang.readinfo.BindData(String bid) rn Wendang.readinfo.Page_Load(Object sender, EventArgs e) rn System.Web.UI.Control.OnLoad(EventArgs e) +67 rn System.Web.UI.Control.LoadRecursive() +35 rn System.Web.UI.Page.ProcessRequestMain() +750 rn
出错在哪?已有打开与此命令相关联的 DataReader,必须首先将它关闭
rnrn//SQL语句查询,返回操作是否成功,数据结果集,错误信息 rn public static bool RetriveDataSet(string strsql,string dsname,ref DataSet ds,ref string ErrMessage) rn rn if (strsql == null || strsql == string.Empty) rn rn ErrMessage = "生成数据集失败,查询字符串为空"; rn return false; rn rnrn if (OraConn == null) rn rn ErrMessage = "生成数据集失败,未连接数据库"; rn return false; rn rnrn using (SqlDataAdapter da = new SqlDataAdapter(strsql, OraConn)) rn rn try rn rn da.Fill(ds,dsname);//出错在此 提示:<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em> rnrn rn catch (SqlException e) rn rn ErrMessage = "生成数据集失败,请检查数据库"; rn return false; rn rn return true; rn rnrn rnrn每次使用SqlDataReader Read()之后我都给<em>关闭</em>了,以下两个方法用到。 rnpublic static void DBToFile(string str_metaid) rn rnrn string strImgSQL = ""; rn string filename=@"c:\imgmetatemp.txt"; rn byte[] File = null; rnrn strImgSQL = @"SELECT detail_metadata FROM img_metadata WHERE meta_id = '" + str_metaid + "'"; rn SqlDataReader Filedr=null; rn SqlCommand oraImagetoDBcmd = new SqlCommand(); rn oraImagetoDBcmd.CommandType = CommandType.Text; rn oraImagetoDBcmd.CommandText = strImgSQL; rn oraImagetoDBcmd.Connection = OraConn; rnrn try rn rn Filedr = oraImagetoDBcmd.ExecuteReader(); rn rn catch (SqlException imageE) rn rn MessageBox.Show(imageE.Message); rn //return null; rn rnrn if (Filedr.HasRows) rn rn Filedr.Read(); rn File = (byte[])Filedr[0]; rnrn FileStream fs = new FileStream(filename, FileMode.Create); rn BinaryWriter bw = new BinaryWriter(fs); rn bw.Write(File, 0, File.Length); rn bw.Close(); rn fs.Close(); rn System.Diagnostics.Process.Start("notepad.exe", @"c:\imgmetatemp.txt"); rnrn rn else rn rn MessageBox.Show("对不起,没有找到元数据文件信息"); rn rn Filedr.Close(); rn rn rnrn public static Image DBToImage(string str_metaid) rn rn rn string strImgSQL = ""; rnrn strImgSQL = @"SELECT dataview_content FROM img_quickview WHERE meta_id = '" + str_metaid + "'"; rn SqlDataReader dr ; rn SqlCommand oraImagetoDBcmd = new SqlCommand(); rn oraImagetoDBcmd.CommandType = CommandType.Text; rn oraImagetoDBcmd.CommandText = strImgSQL; rn oraImagetoDBcmd.Connection = OraConn; rnrn try rn rn dr = oraImagetoDBcmd.ExecuteReader(); rn rn catch (SqlException imageE) rn rn MessageBox.Show(imageE.Message); rn return null ; rn rnrn if (dr.HasRows) rn rn dr.Read(); rn MemoryStream msbuf = new MemoryStream((byte[])dr[0]); rn Image image = Image.FromStream(msbuf, true); rn return image; rn rn dr.Close(); rn return null; rn
dr.close()的用途(必要性)—”已有打开与此命令相关联的 DataReader,必须首先将它关闭。"
在执行数据库的操作时我们会遇到,建立一个 SqlConnection con, 但是多个SqlCommand命令同时使用该con: 如: SqlCommand cmd1 = new SqlCommand(sql1, con);      SqlDataReader dr=cmd1.ExecuteReader();  //注意! SqlCommand cmd2 = new SqlComman
已有打开与此连接相关联的 DataReader,必须首先将它关闭"错误">"已有打开与此连接相关联的 DataReader,必须首先将它关闭"错误
string strConn="server=(local);database=test;uid=sa;pwd=;";rnSqlConnection cn=new SqlConnection(strConn);rncn.Open();rnstring sql="SELECT * FROM test";rnSqlCommand cm=new SqlCommand(sql,cn);rnSqlDataReader dr=cm.ExecuteReader();rnwhile(dr.Read())rnrn string sql2="SELECT * FROM test WHERE ID2="+dr["ID"];rn SqlCommand cm2=new SqlCommand(sql2,cn);rn SqlDataReader dr2=cm.ExecuteReader();rn if(dr2.Read())rn Response.Write("ID:"+dr2["ID"]+" ");rn dr2.Close();rnrndr.Close();rncn.Close();rnrn以上代码有错误,错误发生在:SqlDataReader dr2=cm.ExecuteReader();说什么"<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>",不明白..对象dr和dr2有什么关系?向各位请教一下是什么原因,如何解决这个问题?
菜鸟请求帮助!!已有打开与此命令相关联datareader 必须首先将它关闭
DataTable dtErpSynchSaleContract = new DataTable();rn SqlDataAdapter da = new SqlDataAdapter(cmd);rn da.Fill(dtErpSynchSaleContract);rnrn rn sql = "select " + _SynchMainFields + " from " + _ErpBackupMainTable;rn OracleCommand oracmd = this.CreateCommandFromGateway();rn oracmd.CommandText = sql;rnrn DataTable dtErpBackupSaleContract = new DataTable();rn OracleDataAdapter orada = new OracleDataAdapter(oracmd);rn orada.Fill(dtErpBackupSaleContract);rnrn foreach (DataRow dr in dtErpSynchSaleContract.Rows)rn rn dtErpBackupSaleContract.LoadDataRow(dr.ItemArray, false);rn rnrn OracleCommand insertCommand = this.CreateInsertCommandForGateway(rn _ErpBackupMainTable,rn _SynchMainFields,rn _SynchMainFields);rnrn orada = new OracleDataAdapter();rn orada.InsertCommand = insertCommand;rn orada.Update(dtErpBackupSaleContract);rnrn //置标志位rn sql = "update " + _ErpSynchDetailTable + " set syn_flag = 1 where item_code is not null";rn cmd = this.CreateCommandFromErp();rn cmd.CommandText = sql;rn cmd.ExecuteNonQuery();rnrn //导明细表 rn sql = "select " + _SynchDetailFieldsForErp + " from " + _ErpSynchDetailTable + " where syn_flag=1";rn cmd = this.CreateCommandFromErp();rn cmd.CommandText = sql;rnrn SqlDataReader dtr = cmd.ExecuteReader();rnrn sql = "select " + _SynchDetailFields + " from " + _ErpBackupDetailTable;rn oracmd = this.CreateCommandFromGateway();rn oracmd.CommandText = sql;rnrn DataTable dtErpBackupSaleContractDetail = new DataTable();rn orada = new OracleDataAdapter(oracmd);rn orada.Fill(dtErpBackupSaleContractDetail);rnrn while (dtr.Read())rn rn object[] objs = new object[dtr.FieldCount];rnrn for (int i = 0; i < objs.Length; i++)rn rn //遇到不能处理的数据跳过去rn tryrn rn objs[i] = dtr.GetValue(i);rn rn catch rn rn dtErpBackupSaleContractDetail.LoadDataRow(objs, false);rn rnrn insertCommand = this.CreateInsertCommandForGateway(rn _ErpBackupDetailTable,rn _SynchDetailFields,rn _SynchDetailFields);rnrn orada = new OracleDataAdapter();rn orada.InsertCommand = insertCommand;rn orada.Update(dtErpBackupSaleContractDetail);rnrn //执行初始化存储过程rn OracleParameter parm = new OracleParameter("ret", OracleType.Number);rn parm.Direction = ParameterDirection.ReturnValue;rnrn this.ProcedureFromGateway(_StoreProcedure, new OracleParameter[] parm );rnrn if (parm.Value.ToString() != "0")rn throw new ApplicationException("调用存储过程" + _StoreProcedure + "失败!");rn elsern rn rn dtr.Close();rn rn this.ExecuteFromErp("delete from " + _ErpSynchMainTable + " where syn_flag=1 ");rn this.ExecuteFromErp("delete from " + _ErpSynchDetailTable + " where syn_flag=1");rn rn this.CommitGatewayTransaction();rn this.CommitErpTransaction();rnrn //日志rn new LogManager().AddLog(1, _SynchType, "增量同步成功!");rnrn rn catch(Exception e) rn rn this.RollbackErpTransaction();rn this.RollbackGatewayTransaction();rn //日志rn new LogManager().AddLog(0, _SynchType, "增量同步失败:" + e.Message);rnrn rn finallyrn rn this.CloseErpConnection();rn this.CloseGatewayConnection();rn rnrn ,谈出如标题上的错误,没加事务前是可以运行的rn
老问题:“已有打开与此连接相关联的 DataReader,必须首先将它关闭”,高手请进
在Asp.Net中,感觉SqlDataReader还是挺好用的。rn但最近还是出现了这个问题。。rn页面A有不少地方都使用到了这个对象,我都已经将它<em>关闭</em>了。单个用户进程访问这个页面时不会出现出错的提示。两个以上的用户同时刷新这个页面就出错了。rn我的SqlConnection是在一个组件里调用的,组件例子如下:rnnamespace XXXXrnrn public abstract class SqlConnrn rn static SqlConnection Conn;rn public static bool XXOpenConn()rn rn Conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);rn if (Conn.State != ConnectionState.Open)rn rn tryrn rn Conn.Open();rn return true;rn rn catch(SqlException)rn rn return false;rn rn rn return true;rn rn public static SqlConnection getConn()rn rn if (XXOpenConn())rn return Conn;rn elsern return null;rn rn public static SqlDataReader XXDataReader(string tSQL, SqlConnection tConn)rn rn SqlDataReader tReader;rn SqlCommand tCmd = new SqlCommand(tSQL, tConn);rn tReader = tCmd.ExecuteReader();rn tCmd.Dispose();rn return tReader;rn rn public static Hashtable GetSysConfig()rn rn string tSQL = "select A from s_config";rn Hashtable configHT = new Hashtable();rn SqlConnection meConn = getConn();rn SqlDataReader configReader = XXDataReader(tSQL, meConn);rn if (configReader.Read())rn rn configHT["A"] = configReader["A"].ToString();rn rn elsern rn configHT["A"] = "";rn rn configReader.Close();rn meConn.Close();rn return configHT;rn rn rnrn以上是组件调用Conn已经一些DataReader的方法。。rn外部的页面基本上是公用一个Conn连接,有同时使用到多个DataReader的我都会新开一个Conn连接,Read之后也都<em>关闭</em>了。rn现在问题是单个用户访问,页面不会出错,多个用户同时<em>打开</em>,就会出错。请问这样的问题应该如何解决?rnrnPS:已确认所有<em>打开</em>的DataReader都有<em>关闭</em>掉。rn谢谢
: System.InvalidOperationException: 已有打开与此连接相关联的 DataReader,必须首先将它关闭
sqlConn = new SqlConnection(BP.DataAccess.Constants.ConnectString); string sql = "select count(name)as c,name from get_movie where datediff(dd,datet,getdate()) );rn rnrnrnrnrnrnrn--------------rnrnrn行 73: sqlConn.Open();rn行 74: read = cmd.ExecuteReader();rn行 75: read2 = cmd2.ExecuteReader();rn行 76: while(read.Read() && read2.Read())rn行 77: rn rn
C#多线程 报:已有打开与此命令相关联的 DataReader,必须首先将它关闭
一个线程跑的时候没有任何问题 。。。一开多线程就会报:<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnrnrn private static SqlConnection connection;rnrn public static SqlConnection Connectionrn rn getrn rn string connectionString = con;rn if (connection == null)rn rn connection = new SqlConnection(connectionString);rn connection.Open();rn rn else if (connection.State == System.Data.ConnectionState.Closed)rn rn connection.Open();rn rn else if (connection.State == System.Data.ConnectionState.Broken)rn rn connection.Close();rn connection.Open();rn rn return connection;rn rnrnrn我知道是static SqlConnection connection 的问题。。rnrn但我不知道该怎么改了 新手 。。学习中。。请大虾指点下。。给出代码 。。谢谢rn
SQL事件处理 指示“已有打开与此命令相关联的 DataReader,必须首先将它关闭”。
下面是源代码,各位给看看要怎么改 我如果直接在中间加个加个conn.Close(); 也会提示出错。整个事务的功能是先将数据读出,修改后写回数据库。谢谢rnrn[code=C#]conn.Open();//采用事件处理时行数据提交rn SqlCommand cmd = new SqlCommand();rn SqlTransaction sqltran = conn.BeginTransaction();rn cmd.Connection = conn;rn cmd.Transaction = sqltran;rnrn cmd.CommandText = "SELECT [" + outdept + "],[" + outdept1 + "],[" + indept + "] FROM [Order] where [ClientOrder]='" +lvw_list.Items[1].SubItems[2].Text + "'";rn SqlDataReader getnumber = cmd.ExecuteReader();rn getnumber.Read();rn outi = Convert.ToInt32(getnumber[0].ToString()) -Convert.ToInt32( lvw_list.Items[0].SubItems[4].Text);rn outo = Convert.ToInt32(getnumber[1].ToString()) +Convert.ToInt32( lvw_list.Items[0].SubItems[4].Text);rn ini = Convert.ToInt32(getnumber[2].ToString()) + Convert.ToInt32(lvw_list.Items[0].SubItems[4].Text);rn cmd.CommandText = "UPDATE [Order] SET [" + outdept + "]='" + outi + "',[" + outdept1 + "]='" + outo + "',[" + indept + "]='" + ini + "' WHERE [ClientOrder]='" + lvw_list.Items[1].SubItems[2].Text + "' ";rn cmd.ExecuteNonQuery();rn sqltran.Commit();rn lvw_list.Items.RemoveAt(1);rn conn.Close();[/code]
已有打开与此命令相关联的 DataReader,必须首先将它关闭问题
// 判断总分的优良等级rn SqlConnection con3 = new SqlConnection("Server=.;database=QINXINJITUAN;uid=sa;pwd=12345;");rn SqlDataReader dr1 = null;rn con3.Open();rn string score = "select details from FM_HUIZONG where item='score'";rn SqlCommand cmd10 = new SqlCommand(score, con3);rn dr1 = cmd10.ExecuteReader();rn while (dr1.Read())rn rn int s = Convert.ToInt32(dr1[0]);rn if (s >= 90)rn rn string score1 = "update FM_HUIZONG set DPT='优秀'where item='score'";rn SqlCommand cmd11 = new SqlCommand(score1, con3);rn cmd11.ExecuteNonQuery();rn rn if (s >= 75 && s < 90)rn rn string score2 = "update FM_HUIZONG set DPT='良好'where item='score'";rn SqlCommand cmd12 = new SqlCommand(score2, con3);rn cmd12.ExecuteNonQuery();rn rn if (s >= 60 && s < 75)rn rn string score3 = "update FM_HUIZONG set DPT='中等'where item='score'";rn SqlCommand cmd13 = new SqlCommand(score3, con3);rn cmd13.ExecuteNonQuery();rn rn if (s >= 45 && s < 60)rn rn string score4 = "update FM_HUIZONG set DPT='较差'where item='score'";rn SqlCommand cmd14 = new SqlCommand(score4, con3);rn cmd14.ExecuteNonQuery();rn rn if (s ;rn SqlCommand cmd15 = new SqlCommand(score5, con3);rn cmd15.ExecuteNonQuery();rn rn rn rn rn rn catch (Exception ex)rn rn Response.Write(ex.Message);rn rn这段代码提示“<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>”。可是我不知道怎么修改,是不是之前的代码也要贴上来。谢谢大家了
[异常] 已有打开与此命令相关联的 DataReader,必须首先将它关闭
我在 GetDataSet 中未使用到 DataReader,未何在错误中会经常报标题中的错误,找了好久答案,请各位兄弟帮忙看一下如何解决?rnwebservice 上的数据库连接定义如下:rn rn public static string strConnection = System.Configuration.ConfigurationSettings.AppSettings["SqlStr"];rn rn private static SqlConnection conn = null;rnrn public void open()rn rn if (conn == null)rn rn conn = new SqlConnection(strConnection);rn rn if (conn.State == ConnectionState.Closed)rn rn conn.Open();rn rn rnrn public void close()rn rn if (conn != null||conn.State==ConnectionState.Open)rn rn conn.Close();rn rn rnrn public DataSet GetDataSet(string sql)rn rn open();rnrn if (conn == null) return null;rn tryrn rn DataSet ds = new DataSet();rn SqlDataAdapter sda = new SqlDataAdapter();rn SqlCommand sqlcmd = new SqlCommand();rn sqlcmd.Connection = conn;rn sqlcmd.CommandText = sql;rn sqlcmd.CommandTimeout = 300;rn sda.SelectCommand = sqlcmd;rn sda.Fill(ds);rn return ds;rn rn catch (Exception ex)rn rn log(ex.Message + ":Sql >>" + sql);rn return null;rn rn finallyrn rn if (conn.State == ConnectionState.Open)rn rn conn.Close();rn rn rn
已有打开与此连接相关联的 DataReader,必须首先将它关闭,请问各位如何解决啊?
寻求帮助:请问这是怎么回事? rn<em>打开</em>一个数据库中主表(table1)——从表(table2)的程序如下: rnrnOleDbCommand cmd; rnOleDbDataReader dr; rnOleDbCommand cmd2; rnOleDbDataReader dr2; rnrncmd=new OleDbCommand("select * from table1",cn); rndr=cmd.ExecuteReader(); rnwhile(dr.Read()) rn rnrn cmd2=new OleDbCommand("select * from table2 where blockid=" + rndr.GetInt32(0) +";",cn); rnrn //程序总是在这里出错,错误信息如下: rn //<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em> rn dr2=cmd2.ExecuteReader(); rnrn while(dr2.Read()) rn rn //代码略... rnrn rn dr2.Close(); rn dr2=null; rn cmd2.Dispose(); rn cmd2=null; rnrn rnrndr.Close(); rndr=null; rncmd.Dispose(); rncmd=null; rncn.Dispose(); rncn=null rnrnrn感觉好象DataReader在同一时间只能<em>打开</em>一个??? rn谁能说说这是怎么回事?谢谢先! rnrn
操作失败:已有打开与此连接相关联的 DataReader,必须首先将它关闭
我做了一个新闻抓取机,原理很简单就不说了rn用5个线程同时<em>打开</em>写有具体抓取代码的class,rn在这个class里面抓取内容都是用sqlcmd.ExecuteNonQuery() insert到sqlserver数据库里面.rn但是开始还好,抓了1000条以后5个线程都频繁报'操作失败:<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>'的错误, 并没有什么规律,我并未用到<em>datareader</em>啊rn请指点rn谢谢!
已有打开与此命令相关联的DataReader,必须首先将它关闭"问题解决">"已有打开与此命令相关联的DataReader,必须首先将它关闭"问题解决
我们在进行数据库方面的编码过程中,可能会碰见这样的问题: <em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em> 比如说按如下的代码进行数据库操作: string selStr = "select DeptNAME from Departments where deptid='" + ((CurrentUser)Session["CurrentUser"]).DeptID.To
.Net数据库访问问题:已有打开与此连接相关联的 DataReader,必须首先将它关闭
当创建多个DataReader对象.时,系统将提示“<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>,并不是我想多创建几个这样的对象,只因数据是按树状结构分布的,通过递归来完成数据的访问,所以才会出现多个DataReader的情况,我把问题分析有如下几种可能性:1.数据库本身的限制,这种情况不可能,我在ASP中采用的ODBC访问,没出现问题,转到.NET时才出现这样的情况。2.我用
已有打开与此命令相关联的 DataReader,必须首先将它关闭——什么原因啊???
<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>——什么原因啊???rnrnrn提示错误如下:rnrnrn“/”应用程序中的服务器错误。rn--------------------------------------------------------------------------------rnrn<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。 rn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: System.InvalidOperationException: <em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnrn源错误: rnrnrn行 40: string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";rn行 41: SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);rn行 42: cmd1.ExecuteNonQuery();rn行 43: rn行 44: Response.Write(" ");rn rnrn源文件: f:\wyx3.0\wyxback\admin_adminadd.aspx.cs 行: 42 rnrnrn源码:rnrn[code=C#] protected void Button1_Click(object sender, EventArgs e)rn rn string strConn = ConfigurationManager.ConnectionStrings["SperConnectionString1"].ToString();rn SqlConnection ConnSql = new SqlConnection(strConn);rn ConnSql.Open();rn string strSQL = "SELECT * FROM wyx_user WHERE wyx_name='" + TextBox1.Text + "'";rn SqlCommand cmd = new SqlCommand(strSQL, ConnSql);rn SqlDataReader dr = cmd.ExecuteReader();rn if (dr.Read())//如果用户名存在rn rn Response.Write(" ");rnrn rn elsern rn string password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "MD5");rnrn string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";rn SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);rn cmd1.ExecuteNonQuery();rnrn Response.Write(" ");rn rnrn dr.Close();rn ConnSql.Close();rnrnrn Response.Redirect("admin_admin.aspx");//返回管理页面rnrn [/code]rnrnrn说明一下,这个程序的功能是添加管理员rnrn数据库里的管理员表是wyx_userrnrn我估计是if语句有问题,因为要考虑到如果添加的用户名已经存在,就要给出提示信息
出现提示:已有打开与此连接相关联的 DataReader,必须首先将它关闭
如题,请问怎么解决?先谢了!
求助,已有打开与此连接相关联的 DataReader,必须首先将它关闭,解决马上结分
tryrn rn con.ConnectionString=ConfigurationSettings .AppSettings ["Sqlstr"].ToString ();rn cmd.Connection =con;rn cmd.CommandType =System.Data .CommandType .StoredProcedure ;rn cmd.CommandText ="USERINFO_USERNAME_LIST";rn cmd.Parameters .Add (new SqlParameter ("@username",SqlDbType.NVarChar ,50));rn cmd.Parameters ["@username"].Value ="yy";rn con.Open ();rn cmd.Transaction =con.BeginTransaction ();rn rn SqlDataReader rdr=cmd.ExecuteReader ();rn if(rdr.Read ())rn rn db=new USERINFO (rdr.GetInt32 (0),rdr.GetString (1),rdr.GetString (2),null,null,DateTime.Now ,nullrn ,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,rn null,null,null,null,null);rn rn cmd.Parameters .Clear ();rn cmd.Transaction .Commit ();rn IList bb=new ArrayList ();rn bb.Add (db.UserId);rn bb.Add (db.UserName);rn bb.Add (db.UserPwd );rn ListBox1 .DataSource=bb;rn ListBox1 .DataBind ();rnrnrn rn catch(Exception ex)rn rn cmd.Transaction .Rollback ();rn Response.Write (" ");rn rn finallyrn rn con.Close ();rn rnrnrnrn报错的地方就是回滚那句,说是。rn应该是前面有错误,然后说回滚的时候出错,我想回滚语句,应该写在catch里面吧。rn大家帮忙看一下好吧。我第一天用DATAREADER。谢谢了。。rn总是说我command没关,或是什么,可是我明明关了啊。。谢谢大家了。
已有打开与此连接相关联的 DataReader,必须首先将它关闭。"是什么错误啊?">"已有打开与此连接相关联的 DataReader,必须首先将它关闭。"是什么错误啊?
是不是说我一个方法里面不能用两个SqlDataReader?rn我可是取到一个SqlDataReader后<em>关闭</em>CONN连接,然后再<em>打开</em>连接取另外一个SqlDataReader的阿
出错:已有打开与此连接相关联的 DataReader,必须首先将它关闭
明明已经<em>关闭</em>了DbRUser,重新填充时为什么还有这个错误?rnrn '画用户树,从UserDepartment和User表中取数rn With Tv1rn .Nodes.Add("用户列表")rnrn Dim DbCmd As New SqlClient.SqlCommand("select * from UserDepartment order by [id] ", Cn)rn Dim DbR As SqlClient.SqlDataReaderrn Dim DbCmdUser As New SqlClient.SqlCommandrn Dim DbRUser As SqlClient.SqlDataReaderrn Dim NodeSel As TreeNodernrn DbR = DbCmd.ExecuteReader(CommandBehavior.CloseConnection)rn Do While DbR.Readrn NodeSel = .Nodes.Add(DbR.Item("部门"))rn 'DbCmdUser = New SqlClient.SqlCommandrn DbCmdUser.CommandText = "select * from User where 部门ID=" & DbR.Item("ID")rn DbCmdUser.Connection = Cnrn DbRUser = DbCmdUser.ExecuteReader(CommandBehavior.CloseConnection) '这句出错:<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rn Do While DbRUser.Readrn NodeSel.Nodes.Add(DbRUser("姓名"))rn Looprn DbRUser.Close()rn Looprn End Withrnrnrnrn End Subrnrn
关于“已有打开与此连接相关联的 DataReader,必须首先将它关闭
程序中有一个connection一直循环读取数据而未<em>关闭</em>rn另开一个connection,读取某个数据却出现“<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>”rn这是怎么回事,不同connection下的ExecuteReader()或ExecuteScalar也会互相影响吗??
急!已有打开与此命令相关联的 DataReader,必须首先将它关闭。 asp.net
源代码:rnrnrnpublic partial class forget_psw2 : System.Web.UI.Pagernrn string conStr = "server=.\\sqlexpress;database=User;uid=sa;pwd=123;";rn rn protected void Page_Load(object sender, EventArgs e)rn rn if (!IsPostBack)rn rn SqlConnection con = new SqlConnection(conStr);rn con.Open();rn rn string sqlQuery = "select * from userinfo where userName='" + Request.Params["id"].ToString() + "'";rn SqlCommand cmd = new SqlCommand(sqlQuery, con);rn SqlDataReader dr = cmd.ExecuteReader();rnrn while (dr.Read())rn rn TextBox1.Text = dr["question"].ToString();rn rn dr.Close();rn con.Close();rn rn rn protected void Button1_Click(object sender, EventArgs e)rn rn //一个连接只能<em>打开</em>一个<em>datareader</em> rn SqlConnection con = new SqlConnection(conStr);rn con.Open();rn string sqlQuery = "select * from userinfo where userName='" + Request.Params["id"].ToString() + "'";rn SqlCommand cmd = new SqlCommand(sqlQuery, con);rn SqlDataReader dr = cmd.ExecuteReader();rn rn rn while (dr.Read())rn rnrn rn if (TextBox2.Text.Trim() == dr["answer"].ToString().Trim())rn rn //Response.Redirect("forget_psw2.aspx?id='" + Request.Params["id"].ToString() + "");rn sqlQuery = "update userinfo set password = '" + TextBox3.Text + "'";rn cmd.CommandText = sqlQuery;rn rn if (cmd.ExecuteNonQuery() > 0)rn rn this.Response.Write(" ");rn rn elsern rn this.Response.Write(" ");rn rn rn rn elsern rn this.Response.Write(" ");rn TextBox2.Text = "";rn TextBox3.Text = string.Empty;rn rn rn rn rn con.Close();rn dr.Close();rn rn
新人请教,已有打开与此命令相关联的 DataReader,必须首先将它关闭
<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rn请问如何解决呢,这个执行过程的代码是放在一个循环体内,循环到快完的时候就出来错误。rn[code=C#]public int exep_getrecord(string spstr,string macno,int mactype,string empno,int verifymode,int dwinoutmode,string date,string time,int workcode)rn rn int ret = 0;rn tryrn rn string connectionString = Properties.Settings.Default.standConnectionString.ToString();rn SqlConnection conpro = new SqlConnection(connectionString); rn //con.ConnectionTimeout = 0;rn SqlDataAdapter da = new SqlDataAdapter();rn using(da.SelectCommand = new SqlCommand());rn da.SelectCommand.Connection = conpro;rn da.SelectCommand.CommandText = spstr;rn da.SelectCommand.CommandType = CommandType.StoredProcedure;rn da.SelectCommand.Parameters.Add(new SqlParameter("@macno", macno));rn da.SelectCommand.Parameters.Add(new SqlParameter("@mactype", mactype));rn da.SelectCommand.Parameters.Add(new SqlParameter("@dwenrollnumber", empno));rn da.SelectCommand.Parameters.Add(new SqlParameter("dwverifymode", verifymode));rn da.SelectCommand.Parameters.Add(new SqlParameter("dwinoutmode", dwinoutmode));rn da.SelectCommand.Parameters.Add(new SqlParameter("dwdate", date));rn da.SelectCommand.Parameters.Add(new SqlParameter("dwtime", time));rn da.SelectCommand.Parameters.Add(new SqlParameter("dwworkcode", workcode));rn da.SelectCommand.CommandTimeout = 0;rnrnrn conpro.Open();rn da.SelectCommand.ExecuteReader(CommandBehavior.CloseConnection);rn da.SelectCommand.ExecuteNonQuery();rn conpro.Close();rn //con.Dispose();rn rn catch (SqlException e)rn rn ret++;rn rn finallyrn rn rn rn return ret;rnrn rnrn [/code] rn
求助已有打开与此命令相关联的 DataReader,必须首先将它关闭
[code=C#]rn private void button1_Click(object sender, EventArgs e)rn rn if(textBox2.Text.Trim()!=""&&textBox3.Text.Trim()!=""&&textBox4.Text.Trim()!=""&&textBox6.Text.Trim()!=""&&textBox7.Text.Trim()!=""&&textBox8.Text.Trim()!="")rn rn SqlDataReader sdr = Book.QueryBookName(textBox2.Text.Trim());rn if (sdr.HasRows)rn rn MessageBox.Show("该图书名已经存在!");rn rn elsern rn BookModel.BookName = textBox2.Text.Trim();rn BookModel.Press = comboBox1.SelectedItem.ToString();rn BookModel.ISBN = textBox3.Text.Trim();rn BookModel.Category = comboBox2.SelectedItem.ToString();rn BookModel.PublicationTime = DateTime.Parse(textBox4.Text.Trim());rn BookModel.Writer = textBox6.Text.Trim();rn BookModel.BookPrice =float.Parse(textBox7.Text.Trim());rnrn Book.AddBook(BookModel,int.Parse(textBox8.Text.Trim()),DateTime.Parse(textBox10.Text.Trim()));rn MessageBox.Show("添加图书成功!");rn rn rn rn[/code]rn[code=C#]rn public SqlDataReader QueryBookName(string BookName)rn rn string sqltext = @"select BookName from BookInventory where BookName='" + BookName + "'";rn return SQLDBHelper.ExecuteDR(sqltext);rn rn[/code]rn[code=C#]rn #region 执行SQL命令,返回DataReader对象rn public SqlDataReader ExecuteDR(string sqltext)rn rn this.Open();rn SqlCommand cmd = new SqlCommand(sqltext, con);rn SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);rn return sdr;rn rn #endregionrn[/code]rn[code=C#]rn public void AddBook(BookInventory BookModel, int count, DateTime BookTime)rn rn BookDAL.AddBook(BookModel, count, BookTime);rn rn[/code]rn[code=C#]rn public class BookDALrn rn SQLDBHelper SQLDBHelper = new SQLDBHelper();rn public void AddBook(BookInventory BookModel, int count, DateTime BookTime)rn rn SqlParameter[] parms =rn new SqlParameter ("@图书名",BookModel.BookName),rn new SqlParameter ("@出版社",BookModel.Press),rn new SqlParameter ("@图书ISBN",BookModel.ISBN),rn new SqlParameter ("@图书类别",BookModel.Category),rn new SqlParameter ("@出版时间",BookModel.PublicationTime),rn new SqlParameter ("@作者",BookModel.Writer),rn new SqlParameter ("@图书价格",BookModel.BookPrice),rn new SqlParameter ("@图书数量",count),rn new SqlParameter ("@入库时间",BookTime),rn ;rn SQLDBHelper.ExecuteStoreProc("Pro_Addbook", parms);rn rn rn[/code]rnrn未处理的“System.InvalidOperationException”类型的异常出现在 System.Data.dll 中。rnrn其他信息: <em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rn百度了好久,没解决到啊。求大神
已有打开与此命令相关联的 DataReader,必须首先将它关闭。"的解决方案">网站出现"已有打开与此命令相关联的 DataReader,必须首先将它关闭。"的解决方案
解决方案有两种: 1、数据库为SQL Server 2005版本时,可以在web.config数据库链接串中加入MultipleActiveResultSets=true。 2、进行重复操作之前,将数据查询结果放入内存中,再进行使用。       我的web.config
entity framework中 已有打开与此 Command 相关联的 DataReader,必须首先将它关闭
處理方式有兩種 1,設定ConnectionString加上MultipleActiveResultSets=true,但只適用於SQL 2005以後之版本 2.先讀出放置在List中 foreach(Employee emp in context.Employees.ToList()) {   emp .Customer .... //通過,因emp這時候已經不是conte
已有打开与此命令相关联的 DataReader,必须首先将它关闭 ”错误解决方法
点击一行listview数据时,程序从数据库中读取相关数据,由于两次点击的时间间隔过短,造成一条sql语句查询结果还未返回时下一次的查询命令又需要执行了,产生了“<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em> ”错误。我的解决方法是在数据连接字符串最后加了一句话“MultipleActiveResultSets=True”,例如:string connection_stri
重复调用Fill,提示:已有打开与此命令相关联的 DataReader,必须首先将它关闭
我用VS2008的数据集设计器添加了一个新查询,FillById,GetDataId(@id)rnrn想通过这个返回符合ID条件的单条数据,但是DataSet第一次用。概念<em>有点</em>不清楚。。rnrn我直接在程序里用rnOrderTableAdapter.FillByID(jcmsDataSet.Order, GlobalVar.Id);rnrn运行后提示 <em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnrn因为窗口初始化的时候已经用rnOrderTableAdapter.Fill(jcmsDataSet.Order); 填充数据到 dataGridView1 里了rnrn使用OrderTableAdapter.Connection.Close(): 也没用。。
错误:已有打开与此命令相关联的 DataReader,必须首先将它关闭
新做程序出了常出的错误,但怎么也查不到原因。rn正常一个人登陆,同时开几十个也不会报错,但我们4个人一起不断的<em>打开</em>,就会有如上错误。rn<em>奇怪</em>的是,如果程序中只有一个地方调用也不会错,我有两个控件同时调用数据库就会错。rnrn具体数据库调用类如下:DBTrans.csrnusing System;rnusing System.Collections.Generic;rnusing System.Text;rnusing System.Data.SqlClient;rnusing System.Data;rnusing System.Configuration;rnrnnamespace DALrnrn public class DBTransrn rn private static SqlConnection connection;rn private static SqlTransaction myTrans;rnrn /// rn /// <em>打开</em>连接,并创建事务rn /// rn /// rn private static SqlConnection Connection()rn rn string strSql = ConfigurationManager.ConnectionStrings["MYSCM"].ConnectionString;rn if (connection == null)rn rn connection = new SqlConnection(strSql);rn connection.Open();rn if (myTrans == null)rn myTrans = connection.BeginTransaction();rn elsern rn myTrans.Dispose();rn myTrans = connection.BeginTransaction();rn rn rn else if (connection.State == System.Data.ConnectionState.Closed)rn rn connection.Open();rn if (myTrans == null)rn myTrans = connection.BeginTransaction();rn elsern rn myTrans.Dispose();rn myTrans = connection.BeginTransaction();rn rn rn else if (connection.State == System.Data.ConnectionState.Broken)rn rn connection.Close();rn connection.Open();rn if (myTrans == null)rn myTrans = connection.BeginTransaction();rn elsern rn myTrans.Dispose();rn myTrans = connection.BeginTransaction();rn rn rn return connection;rn rnrn //<em>关闭</em>连接rn public static void CloseConn()rn rn myTrans.Dispose();rn connection.Close();rn rnrn //提交事务rn public static void commitTrans()rn rn myTrans.Commit();rn rnrn //回滚事务rn public static void rollBackTrans()rn rn myTrans.Rollback();rn rnrn public static int ExecuteCommand(string safeSql)rn rn SqlConnection conn = Connection();rn using (SqlCommand cmd = new SqlCommand(safeSql, conn))rn rn cmd.CommandTimeout = 300;rn cmd.Transaction = myTrans;rn int result = 0;rn result = cmd.ExecuteNonQuery();rn cmd.Parameters.Clear();rn return result;rn rn rnrn public static int ExecuteCommand(params SqlParameter[] values)rn rn SqlConnection conn = Connection();rn using (SqlCommand cmd = new SqlCommand())rn rn cmd.Connection = conn;rn cmd.CommandTimeout = 300;rn cmd.Transaction = myTrans;rn cmd.CommandText = "Pro_UpdateBooksCatagory";rn cmd.CommandType = CommandType.StoredProcedure;rn cmd.Parameters.AddRange(values);rn int result = 0;rn result = cmd.ExecuteNonQuery();rn cmd.Parameters.Clear();rn return result;rn rn rnrn public static int ExecuteCommand(string sql, params SqlParameter[] values)rn rn SqlConnection conn = Connection();rn using (SqlCommand cmd = new SqlCommand(sql, conn))rn rn cmd.CommandTimeout = 300;rn cmd.Transaction = myTrans;rn cmd.Parameters.AddRange(values);rn int result = cmd.ExecuteNonQuery();rn cmd.Parameters.Clear();rn return result;rn rn rn rn public static DataTable GetDataSet(string safeSql)rn rn SqlConnection conn = Connection();rn DataSet ds = new DataSet();rn using (SqlCommand cmd = new SqlCommand(safeSql, conn))rn //SqlCommand cmd = new SqlCommand(safeSql, conn);rn cmd.CommandTimeout = 300;rn cmd.Transaction = myTrans;rn SqlDataAdapter da = new SqlDataAdapter(cmd);rn da.Fill(ds);rn cmd.Parameters.Clear();rn return ds != null && ds.Tables.Count > 0 ? ds.Tables[0] : new DataTable();rn rn rnrn public static DataTable GetDataSet(string sql, params SqlParameter[] values)rn rn SqlConnection conn = Connection();rn DataSet ds = new DataSet();rn using (SqlCommand cmd = new SqlCommand(sql, conn))rn //SqlCommand cmd = new SqlCommand(sql, conn);rn cmd.CommandTimeout = 300;rn cmd.Transaction = myTrans;rn cmd.Parameters.AddRange(values);rn SqlDataAdapter da = new SqlDataAdapter(cmd);rn da.Fill(ds);rn cmd.Parameters.Clear();rn return ds != null && ds.Tables.Count > 0 ? ds.Tables[0] : new DataTable();rn rn rnrn rnrnrnDAL层:调用的程序:MaterialService.csrnrnusing System;rnusing System.Collections.Generic;rnusing System.Text;rnusing Models;rnusing System.Data;rnusing System.Data.SqlClient;rnrnnamespace DALrnrn public class MaterialService : BaseControlrn rn /// rn /// 构造函数rn /// rn public MaterialService()rn rnrn rnrn /// rn /// 获取物料类别rn /// rn /// rn /// rn public IList GetMaterKind()rn rn tryrn rn List list = new List();rn string sql = "select distinct 类别=rtrim(类别) from 物料"rn + " union select 类别='' from 物料";rn DataTable dataTable = DBTrans.GetDataSet(sql);rn foreach (DataRow rows in dataTable.Rows)rn rn MaterialInfo materInfo = new MaterialInfo();rn materInfo.Kind = (string)rows["类别"];rn list.Add(materInfo);rn rn return list;rn rn rn catch (Exception e)rn rn throw new ApplicationException("事务操作出错,系统信息:" + e.Message);rn rn rn rnrnrnrnDALCtrl层:调用的程序:MaterialManager.cs rnusing System;rnusing System.Collections.Generic;rnusing System.Text;rnusing Models;rnusing DAL;rnrnnamespace DALCtrlrnrn public class MaterialManagerrn rn public static IList GetMaterKind()rn rn List list = new List();rn MaterialService materialService = new MaterialService();rn tryrn rn list = (List)materialService.GetMaterKind();rn DBTrans.commitTrans();rn DBTrans.CloseConn();rn rn catch (Exception e)rn rn tryrn rn DBTrans.rollBackTrans();rn DBTrans.CloseConn();rn rn catch (Exception ex)rn rn DBTrans.CloseConn();rn throw new ApplicationException("事务回滚出错,系统信息:" + ex.Message);rn rn throw new ApplicationException("事务提交出错,系统信息:" + e.Message);rn rn return list;rn rn rnrnrnrn
已有打开与此连接相关联的 DataReader,必须首先将它关闭"的提示?">在哪几种情况下会出现"已有打开与此连接相关联的 DataReader,必须首先将它关闭"的提示?
<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。 rnrn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: System.InvalidOperationException: <em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnrn源错误: rnrnrn行 75: rn行 76: Dim objReader as OleDbDataReaderrn行 77: objReader = objCmd.ExecuteReaderrn行 78: rn行 79: IF objReader.read() Thenrn rnrn源文件: G:\NewEoffice\PubManage\PubBooks\CNmag\LendBook.aspx 行: 77 rnrn堆栈跟踪: rnrnrn[InvalidOperationException: <em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。]rn System.Data.OleDb.OleDbConnection.SetStateExecuting(OleDbCommand attempt, String method, Boolean flag) +126rn System.Data.OleDb.OleDbCommand.ValidateConnectionAndTransaction(String method, Int32& localState) +103rn System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +61rn System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +57rn System.Data.OleDb.OleDbCommand.ExecuteReader() +7rn ASP.lendbook_aspx.subLend(Object sender, ImageClickEventArgs e) in G:\NewEoffice\PubManage\PubBooks\CNmag\LendBook.aspx:77rn System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +109rn System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +69rn System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18rn System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33rn System.Web.UI.Page.ProcessRequestMain() +1263rnrn rn
在线等待,“已有打开与此命令相关联的 DataReader,必须首先将它关闭。”
public partial class _Default : System.Web.UI.Pagernrn SqlConnection conn = new SqlConnection("Initial Catalog=sy;Data Source=.;integrated Security=true;");rn rn rn protected void Page_Load(object sender, EventArgs e)rn rn rn rn protected void Button1_Click(object sender, EventArgs e)rn rn if (TextBox1.Text ==""&&DropDownList1.SelectedValue=="")rn Response.Write(" "); rn elsern rn if (DropDownList1.SelectedValue == "资迅")rn rn Session["tile"] = TextBox1.Text;rn Response.Redirect("旅游指南.aspx"); rn if (DropDownList1.SelectedValue == "新闻")rn rn Session["tile2"] = TextBox1.Text;rn Response.Redirect("旅游指南.aspx");rn rn if (DropDownList1.SelectedValue == "景点")rn rn string url="景点信息.aspx?id="+TextBox1.Text;rn Response.Redirect(url);rn rn rn rnrnrn protected void ImageButton1_Click(object sender, ImageClickEventArgs e)rn rnrn tryrn rn conn.Open();rn string mySel = "select * from adm where adim='" + TextBox2.Text.Trim() + "' ";rn SqlCommand com = new SqlCommand(mySel, (SqlConnection)conn);rn SqlDataReader reader = com.ExecuteReader();rn if (!reader.HasRows)rn rn Response.Write(" ");rn rn elsern rn while (reader.Read())rn rn if (TextBox3.Text.Trim() != reader.GetString(1).Trim())rn Response.Write(" ");rnrnrn elsern rn Session["adm"] = TextBox2.Text.Trim();rn Response.Redirect("~/后台/后台.aspx");rnrnrnrnrn rn rn rn rn catch (SqlException)rn rn Response.Write(" ");rn rn finallyrn rn if (conn != null)rn conn.Close();rn rn rn protected void ImageButton2_Click(object sender, ImageClickEventArgs e)rn rn TextBox2.Text = "";rn TextBox3.Text = "";rn rn protected void ImageButton3_Click(object sender, ImageClickEventArgs e)rn rn rn if (DropDownList2.SelectedValue == "高级会员")rn string adim = TextBox4.Text;rn string gpwd = TextBox5.Text;rn tryrn rn conn.Open();rn string mySel = "select * from gjhy where 编号='" + adim + "' ";rn SqlCommand com = new SqlCommand(mySel, (SqlConnection)conn);rn SqlDataReader reader = com.ExecuteReader();rn if (!reader.HasRows)rn rn Response.Write(" ");rn rn elsern rn while (reader.Read())rn rn if (gpwd != reader.GetString(7).Trim())rn Response.Write(" "); rnrnrn elsern rn [color=#FF0000] Session["naa"] = TextBox4.Text.Trim();rn string na;rn na = Session["naa"].ToString();rn SqlDataAdapter da = new SqlDataAdapter("select * from gjhy where 编号='" + na + "'", conn);rn DataSet ds = new DataSet();rn [color=#FFFF00]da.Fill(ds);[/color]//这里出错rn if (ds.Tables[0].Rows[0][8].ToString() == "酒店")rn Response.Redirect("上传酒店信息.aspx"); rn if (ds.Tables[0].Rows[0][8].ToString() == "景点")rn Response.Redirect("上传景点信息.aspx"); rn if (ds.Tables[0].Rows[0][8].ToString() == "商场")rn Response.Redirect("上传商场信息.aspx"); rn if (ds.Tables[0].Rows[0][8].ToString() == "旅行社")rn Response.Redirect("上传旅游公司信息.aspx"); [/color]rnrnrn rn rn rn rn catch (SqlException)rn rn Response.Write(" ");rn rn finallyrn rn if (conn != null)rn conn.Close();rn rn rn if (DropDownList2.SelectedValue == "普通会员")rn rn string adim1 = TextBox4.Text;rn string gpwd1 = TextBox5.Text;rn tryrn rn conn.Open();rn string mySel = "select * from hyzc where name='" + adim1 + "' ";rn SqlCommand com = new SqlCommand(mySel, (SqlConnection)conn);rn SqlDataReader reader = com.ExecuteReader();rn if (!reader.HasRows)rn rn Response.Write(" ");rn rn elsern rn while (reader.Read())rn rn if (gpwd1 != reader.GetString(3).Trim())rn Response.Write(" "); rnrnrn elsern rn Session["name"] = TextBox4.Text.Trim();rnrn Response.Redirect("主页.aspx");rnrn rnrnrn rn rn rn rn catch (SqlException)rn rn Response.Write(" ");rn rn finallyrn rn if (conn != null)rn conn.Close();rn rn rn TextBox4.Text = "";rn DropDownList2.Text = "";rn rn protected void ImageButton4_Click(object sender, ImageClickEventArgs e)rn rn if (DropDownList2.SelectedValue == "")rn Response.Write(" "); rn if (DropDownList2.SelectedValue == "高级会员")rn Response.Redirect("高级会员注册.aspx"); rn if (DropDownList2.SelectedValue == "普通会员")rn Response.Redirect("会员注册.aspx"); rn rn
求助:已有打开与此 Command 相关联的 DataReader,必须首先将它关闭
当出现多个用户同时点击的时候会出现此红页,不知道应该如何解决。rn在网上找了方法,rn设定ConnectionString加上MultipleActiveResultSets=true(只适用于SQL 2005以后的版本)rn服务器上安装的SQL版本是2005的,rn我们在web.config中修改后使用依然会出现这种问题,求助。rn[img=http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/17.gif][/img]rnrn[code=csharp]rn using (WorkflowDataSet.SqlWorkflowInstanceDataTable dataTable = new WorkflowDataSet.SqlWorkflowInstanceDataTable()) rn this.Adapter.Fill(dataTable);rn return dataTable;rn rn[/code]rn错误源是this.Adapter.Fill(dataTable),但是我加上using后依然没有用,仍然是这句话报错。求解。
已有打开与此连接相关联的 DataReader,必须首先将它关闭,怎么办?
源代码:rnFor Each files In dir.GetFilesrn SqlConnection1.Open()rnrn SqlCommand1.CommandText = "SELECT * FROM vod WHERE (vname = '" & files.Name & " ')"rn 'SqlDataAdapter1.Fill(DataSet1, "vod")rnrnrn Dim myReader As SqlDataReaderrn myReader = SqlCommand1.ExecuteReader()rnrnrn While myReader.Read() = Falsernrnrnrn SqlCommand1.CommandText = "Select Count(*) From vod"rn newID = CInt(SqlCommand1.ExecuteScalar())rnrnrn fileName = Path.GetFileNameWithoutExtension(files.Name)rn fileType = Path.GetExtension(files.Name)rnrnrn错误信息:rn“/WebApplication1”应用程序中的服务器错误。rn--------------------------------------------------------------------------------rnrn<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。 rn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: System.InvalidOperationException: <em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnrn源错误: rnrnrn行 104:rn行 105: SqlCommand1.CommandText = "Select Count(*) From vod"rn行 106: newID = CInt(SqlCommand1.ExecuteScalar())rn行 107:rn行 108:rn rnrn源文件: C:\Inetpub\wwwroot\WebApplication1\WebForm1.aspx.vb 行: 106 rn
DataAdapter.Fill(dataSet)已有打开与此命令相关联的 DataReader,必须首先将它关闭
[img=http://img.bbs.csdn.net/upload/201611/15/1479180787_443482.jpg][/img][img=http://img.bbs.csdn.net/upload/201611/15/1479180793_958259.jpg][/img][img=http://img.bbs.csdn.net/upload/201611/15/1479180798_951890.jpg][/img][img=http://img.bbs.csdn.net/upload/201611/15/1479180803_913178.jpg][/img][img=http://img.bbs.csdn.net/upload/201611/15/1479180809_133921.jpg][/img][img=http://img.bbs.csdn.net/upload/201611/15/1479180813_94502.jpg][/img][img=http://img.bbs.csdn.net/upload/201611/15/1479180816_915341.jpg][/img][img=http://img.bbs.csdn.net/upload/201611/15/1479180820_546832.jpg][/img]
建立TreeView出错,出错提示:已有打开与此连接相关联的dATArEADER,必须首先将它关闭
public static void AddMenuTreeView(TreeView FTreeView)rn rn string is_menuid="",is_menuname="";rn string is_modelid="",is_modelname="";rn SqlCommand MenuModelSql=new SqlCommand("select menuid,menuname from MENUMODEL",DataBaseConnect.ReturnConnectString);rn SqlDataReader dr_MenuModel ;rn SqlDataReader dr_Menu ; rn dr_MenuModel=MenuModelSql.ExecuteReader();rn while (dr_MenuModel.Read())rn rn is_menuid=dr_MenuModel.GetString(0);is_menuname=dr_MenuModel.GetString(1);rn TreeViewNode cRoot=new TreeViewNode(is_menuname);rn FTreeView.Nodes.Add(cRoot);rn SqlCommand MenuSql=new SqlCommand("select modelid,modelname from view_menu where menuid='"+is_menuid+"'",DataBaseConnect.ReturnConnectString);rn dr_Menu=MenuSql.ExecuteReader();rn while (dr_Menu.Read())rn rn is_modelid=dr_Menu.GetString(0);rn is_modelname=dr_Menu.GetString(1);rn rn rn dr_Menu.Close();rn rn rn rnrn当程序运行到这句时:dr_Menu=MenuSql.ExecuteReader();rn出错:<em>已有</em><em>打开</em>的<em>与此</em>连接<em>相关联</em>的dATArEADER,<em>必须</em><em>首先</em>将它<em>关闭</em>
如何解决“已有打开与此命令相关联的 DataReader,必须首先将它关闭。”
这个异常相信许多的朋友都遇到过,我也经常遇到, 都是编码时大意,没有<em>关闭</em>连接,简单处理就OK之~~      但今天同样的问题,把我郁闷坏了,整个网站操作数据库的地方都报这个错误,出现的频率很高,刷新一下就好了,使得网站不能正常运行。      我仔细检查了几遍,都没发现什么问题,但还是报错,最后没办法,把所有DataReader读取的地方全删之,改成用DataTable存放数据,在费周折,还是不
已有打开与此连接相关联的 DataReader,必须首先将它关闭,怎么解决?
代码如下:rn string strConnection=dataConnection.getStrConnection();rn SqlConnection objConnection=new SqlConnection(strConnection);rn SqlCommand objCommand = new SqlCommand("select itemid from t_dim_power where Userid='"+UserList.SelectedItem.Text+"'" , objConnection); rn SqlCommand objCommand2;rn objConnection.Open();rn SqlDataReader objDataReader; rn objDataReader = objCommand.ExecuteReader();rn while (objDataReader.Read() )rn rn Response.Write(objDataReader[ "itemid" ].ToString()+"@@@");rn objCommand2 = new SqlCommand("insert into t_dim_power(userid,itemid) values('"+usernm.Text+"','"+objDataReader[ "itemid" ].ToString()+"')" , objConnection); rn objCommand2.ExecuteNonQuery();rn rn objDataReader.Close();rn lblMessage.Text = "授权成功!返回用户管理!";rn objConnection.Close();rn objConnection=null;rnrn是不是由于同时在使用objConnection,可是我不知道这个情况怎么避免啊?请高手指教
错误信息:已有打开与此命令相关联的 DataReader,必须首先将它关闭
代码如下:rnpublic class RunAdapterrnrn static private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());rn static private SqlDataAdapter sda = new SqlDataAdapter();rn static public DataTable readData(string sql,SqlParameter[] sp)rn rn sda.SelectCommand = new SqlCommand(sql, con);rn if(sp != null)rn for (int i = 0; i < sp.Length; ++i)rn sda.SelectCommand.Parameters.Add(sp[i]);rn DataTable dt = new DataTable();rn if(con.State == ConnectionState.Closed)rn con.Open();rn sda.Fill(dt);rn con.Close();rn return dt;rn rnrnrn在一个页面中多次调用上面的代码,多数情况下不出错 但是多刷新几次页面就会发生错误,提示信息是:rn错误信息:<em>已有</em><em>打开</em>的<em>与此</em>命令<em>相关联</em>的 DataReader,<em>必须</em><em>首先</em>将它<em>关闭</em>。rnrn我如何<em>关闭</em>“与之<em>相关联</em>的<em>datareader</em>”?
文章热词 机器学习 机器学习课程 机器学习教程 深度学习视频教程 深度学习学习
相关热词 c#打开 关闭线程 c#添加已有窗口 c++修改已有类名 android 仿微信评论回复 求简明python教程 乌班图终端打开python
我们是很有底线的