A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 汪振 中级黑马   /  2013-2-27 09:15  /  3783 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //连接的constr
  2.         private static string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

  3.         public SqlDataReader ExecuteReader(string sql)
  4.         {
  5.             using (SqlConnection conn = new SqlConnection(constr))
  6.             {//到数据库的连接
  7.                 conn.Open();
  8.                 using (SqlCommand cmd = conn.CreateCommand())
  9.                 {//cmd
  10.                     
  11.                     cmd.CommandText = sql;
  12.                     
  13.                     return cmd.ExecuteReader();
  14.                     
  15.                 }
  16.             }
  17.         }
复制代码
以上为SqlHelper那个类。
  1. #region //从数据库读取到编号的值

  2.             SqlHelper sqlhelper=new SqlHelper();
  3.             SqlDataReader reader= sqlhelper.ExecuteReader("select * from Sheet1");
  4.             while (reader.Read())//开始逐条读取table中的数据
  5.             {
  6.                 string ids = reader["" + "编号" + ""].ToString();
  7.                 string isd = reader["编号"].ToString();
  8.                 if (ids != "")
  9.                 {
  10.                     cmbidlist.Items.Add(ids);//将所有的编号读入到可选下啦列表中
  11.                 }
  12.             }
  13.             
  14.             #endregion
复制代码
在Main方法调用它,报错如图,求解原因!{:soso_e183:}

之前我是用下面的代码写的,运行正常。
  1. #region //从数据库读取到编号的值
  2.             string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
  3.             using (SqlConnection conn = new SqlConnection(constr))
  4.             {//到数据库的连接
  5.                 conn.Open();
  6.                 using (SqlCommand cmd = conn.CreateCommand())
  7.                 {//cmd
  8.                     cmd.CommandText = "select * from Sheet1";
  9.                     using (SqlDataReader reader = cmd.ExecuteReader())
  10.                     {
  11.                         //  "select * from Sheet1")
  12.                         while (reader.Read())//开始逐条读取table中的数据
  13.                         {
  14.                             string ids = reader["" + "编号" + ""].ToString();
  15.                             string isd = reader["编号"].ToString();
  16.                             if (ids != "")
  17.                             {
  18.                                 cmbidlist.Items.Add(ids);//将所有的编号读入到可选下啦列表中
  19.                             }
  20.                         }
  21.                     }
  22.                 }
  23.             }
  24.             
  25.             #endregion
复制代码

1.jpg (23.34 KB, 下载次数: 76)

1.jpg

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

5 个回复

倒序浏览
    //封装返回DataReader的方法
    public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
    {
        SqlConnection con = new SqlConnection(constr);
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            if (pms != null)
            {
                cmd.Parameters.AddRange(pms);
            }
            con.Open();
            //当datareader关闭,对应的连接关闭
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
    }

点评

好  发表于 2013-3-7 14:56

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

回复 使用道具 举报
你好,因为你在调用方法完毕后把连接关闭了,所以就读取失败了

解决办法是: 看代码
  1. public static SqlDataReader ExecuteReader(string cmdText, CommandType type, params SqlParameter[] pars)

  2.        {

  3.            //创建连接通道

  4.            SqlConnection conn = new SqlConnection(connStr);

  5.            //创建操作对象

  6.            using (SqlCommand cmd = new SqlCommand(cmdText, conn))

  7.            {

  8.                //操作类型            

  9.                cmd.CommandType = type;

  10.                //添加参数

  11.                if (pars.Length != 0)

  12.                {

  13.                    cmd.Parameters.AddRange(pars);

  14.                }

  15.                //连接数据库

  16.                conn.Open();

  17.                //返回对象,并且等待对象读取完毕时关闭数据库连接

  18.                return cmd.ExecuteReader(CommandBehavior.CloseConnection);

  19.            }

  20.        }
复制代码

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 康晓璞 于 2013-2-28 08:41 编辑


如上图
using语句会产生隐式的try...finally 来处理隐式的异常。
这样在finally语句块退出时,会将资源释放掉。
在本例中using会将数据器的关闭。所以对关闭后的数据器在此调用Read方法,就会出现无效的的异常。
可以使用try....catch
  public static SqlDataReader RTDataReader(string strSql)
        {
            SqlConnection conn = null;
            try
            {
                 conn= new SqlConnection(ConnString);
                SqlCommand cmd = new SqlCommand(strSql,conn);
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                 return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                }
            catch (Exception ex)
            {
                if (conn != null)
                    conn.Close();
                    throw ex;
                 }
        }


点评

嗯,找到原因了,谢谢啊  发表于 2013-2-28 08:34

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

回复 使用道具 举报
戴鑫凯 发表于 2013-2-27 10:14
你好,因为你在调用方法完毕后把连接关闭了,所以就读取失败了

解决办法是: 看代码 ...

SqlCommand cmd = new SqlCommand(cmdText, conn)

这里为什么要用new command 不能用conn来创建一个他的连接么?
回复 使用道具 举报
汪振 发表于 2013-2-28 08:31
SqlCommand cmd = new SqlCommand(cmdText, conn)

这里为什么要用new command 不能用conn来创建一个他的 ...

有conn啊,没有using而已
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马