+ -
当前位置:首页 → 问答吧 → 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。

已有打开的与此命令相关联的 DataReader,必须首先将它关闭。

时间:2011-12-08

来源:互联网

C# code

        public IList<RoadShow> SearchAll()
        {
            IList<RoadShow> list = new List<RoadShow>();
            using (SqlDataReader dr = DBHelper.ExecuteReader("select * from RoadShow"))
            {
                while (dr.Read())
                {
                    RoadShow road = new RoadShow();
                    road.Id = int.Parse(dr["ID"].ToString());
                    road.Answer = bool.Parse(dr["Answer"].ToString());
                    road.Ask = bool.Parse(dr["Ask"].ToString());
                    road.IsAnswer = bool.Parse(dr["IsAnswer"].ToString());
                    road.Contents = dr["Contents"].ToString();
                    road.Ip = dr["Ip"].ToString();
                    road.Datetime = DateTime.Parse(dr["Datetime"].ToString());
                    road.ReplyId = int.Parse(dr["ReplyId"].ToString());
                    RolesDAL dal = new RolesDAL();
                    int rolesid = int.Parse(dr["Roles"].ToString());
                    road.Roles = dal.SearchRolesById(rolesid);
                    list.Add(road);
                }
                dr.Close();
            }            
            DBHelper.Ocon.Close();
            return list;
        }

        public Roles SearchRolesById(int id)
        {
            Roles roles = new Roles();
            SqlParameter par = new SqlParameter("@ID", id);
            using (SqlDataReader dr = DBHelper.ExecuteReader("select * from roles where ID = @ID", par))
            {
                if (dr.Read())
                {
                    roles.Id = (int)dr["ID"];
                    roles.Code = dr["Code"].ToString();
                    roles.Role = dr["Role"].ToString();
                    roles.RoleId = (int)dr["RoleId"];
                }
                dr.Close();
            }            
            DBHelper.Ocon.Close();
            return roles;
        }

        public static SqlConnection Ocon
        {
            get
            {
                string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                if (ocon == null)
                {
                    ocon = new SqlConnection(connectionString);
                    ocon.Open();
                }
                else if (ocon.State == ConnectionState.Closed)
                {
                    ocon.Open();
                }
                else if (ocon.State == ConnectionState.Broken)
                {
                    ocon.Close();
                    ocon.Open();
                }
                return ocon;
            }
        }

        /// <summary>
        /// 执行结果集(非外键表)
        /// </summary>
        /// <param name="safeSql">sql语句</param>
        /// <returns>集合</returns>
        public static SqlDataReader ExecuteReader(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Ocon);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
        /// <summary>
        /// 执行结果集(非外键表)
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="value">sql语句单个参数(对应表中字段)</param>
        /// <returns>集合</returns>
        public static SqlDataReader ExecuteReader(string sql, SqlParameter value)
        {
            SqlCommand cmd = new SqlCommand(sql, Ocon);
            cmd.Parameters.Add(value);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }

我用的是DBHelper类,while循环的时候dr的连接是不能关闭的,查询SearchRolesById方法的时候dr的连接状态的开启的,怎么解决。请高手帮忙!

作者: jy03204340   发布时间: 2011-12-08

代码里面的 DBHelper.Ocon.Close(); 去掉

作者: liao02163595   发布时间: 2011-12-08

DBHelper.Ocon.Close();这句代码就不需要了,因为你使用了using语句,会自动帮你释放掉链接对象的

作者: Andrewsway   发布时间: 2011-12-08

引用 1 楼 liao02163595 的回复:
代码里面的 DBHelper.Ocon.Close(); 去掉

不行啊,去掉没用!

作者: jy03204340   发布时间: 2011-12-08

引用 2 楼 andrewsway 的回复:
DBHelper.Ocon.Close();这句代码就不需要了,因为你使用了using语句,会自动帮你释放掉链接对象的

跟这个没关系,关掉也没用,是dr的状态的问题,思路我知道,就是不知道怎么解决!

作者: jy03204340   发布时间: 2011-12-08

这时因为road.Roles = dal.SearchRolesById(rolesid);的时候,需要同时打开两个阅读器,而你只有一个连接对象,需要把连接对象做成非静态的。

作者: dalmeeme   发布时间: 2011-12-08

引用 5 楼 dalmeeme 的回复:
这时因为road.Roles = dal.SearchRolesById(rolesid);的时候,需要同时打开两个阅读器,而你只有一个连接对象,需要把连接对象做成非静态的。

嗯,是要同时打开两个阅读器,但第2个怎么打开?

作者: jy03204340   发布时间: 2011-12-08

ocon 是定义的静态的?

作者: Sandy945   发布时间: 2011-12-08

第一个阅读器是打开的状态,第二个阅读器又如何打开?
using语句中dr又不可以重新new或者赋值,怎么解决?

作者: jy03204340   发布时间: 2011-12-08

引用 7 楼 sandy945 的回复:
ocon 是定义的静态的?


C# code

private static SqlConnection ocon = null;
        private static string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        public static SqlConnection Ocon

作者: jy03204340   发布时间: 2011-12-08

引用 7 楼 sandy945 的回复:
ocon 是定义的静态的?

C# code

private static SqlConnection ocon = null;
        private static string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        public static SqlConnection Ocon
        {
            get
            {
                string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                if (ocon == null)
                {
                    ocon = new SqlConnection(connectionString);
                    ocon.Open();
                }
                else if (ocon.State == ConnectionState.Closed)
                {
                    ocon.Open();
                }
                else if (ocon.State == ConnectionState.Broken)
                {
                    ocon.Close();
                    ocon.Open();
                }
                return ocon;
            }

作者: jy03204340   发布时间: 2011-12-08

DBHelper 这个类你需要改动

不能用静态的 Connection 对象

作者: Sandy945   发布时间: 2011-12-08

方法可以是静态的,但是变量 尤其是 Connection 对象一定不能是静态的

作者: Sandy945   发布时间: 2011-12-08

引用 11 楼 sandy945 的回复:
DBHelper 这个类你需要改动

不能用静态的 Connection 对象

为什么不能用?

作者: jy03204340   发布时间: 2011-12-08

引用 12 楼 sandy945 的回复:
方法可以是静态的,但是变量 尤其是 Connection 对象一定不能是静态的

理由!!!

作者: jy03204340   发布时间: 2011-12-08

一个连接同时只能打开一个阅读器吧,连接对象不要用静态的,要不这样:
C# code
        public static SqlDataReader ExecuteReader(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, GetConnection());
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
        public static SqlDataReader ExecuteReader(string sql, SqlParameter value)
        {
            SqlCommand cmd = new SqlCommand(sql, GetConnection());
            cmd.Parameters.Add(value);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
public SqlConnection GetConnection()
{
                string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConncection ocon;
                if (ocon == null)
                {
                    ocon = new SqlConnection(connectionString);
                    ocon.Open();
                }
                else if (ocon.State == ConnectionState.Closed)
                {
                    ocon.Open();
                }
                else if (ocon.State == ConnectionState.Broken)
                {
                    ocon.Close();
                    ocon.Open();
                }
                return ocon;
}

作者: dalmeeme   发布时间: 2011-12-08

引用 15 楼 dalmeeme 的回复:
一个连接同时只能打开一个阅读器吧,连接对象不要用静态的,要不这样:

C# code
public static SqlDataReader ExecuteReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, GetConnection());
……

哥们你这代码都是错的!

作者: jy03204340   发布时间: 2011-12-08

引用 14 楼 jy03204340 的回复:
引用 12 楼 sandy945 的回复:
方法可以是静态的,但是变量 尤其是 Connection 对象一定不能是静态的

理由!!!

网站几个人访问?

作者: Sandy945   发布时间: 2011-12-08

引用 17 楼 sandy945 的回复:
引用 14 楼 jy03204340 的回复:
引用 12 楼 sandy945 的回复:
方法可以是静态的,但是变量 尤其是 Connection 对象一定不能是静态的

理由!!!

网站几个人访问?

很多人访问!

作者: jy03204340   发布时间: 2011-12-08

那很多人同时访问,一个静态Connection 怎么处理?

作者: Sandy945   发布时间: 2011-12-08