解决方案 »

  1.   

    com.ExecuteReader();
    connection.Close();
      

  2.   

    catch(Exception)
    {
        throw;
    }
    finally
    {
        connection.Close();
    }
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;
    class sqlHelp
     {
        static string constr = "server=.;database=Dept;uid=sql;pwd=123";
       
        static SqlConnection con = new SqlConnection(constr);
       
         //得到一个数据
       public static object GetOneData(string sql,List<SqlParameter> lt)
       {
           try
           {
               con.Open();
               SqlCommand com = new SqlCommand(sql,con);
               if (lt != null)
               {
                   foreach (SqlParameter sp in lt)
                   {
                       com.Parameters.Add(sp);
                   }
               }
               object b = com.ExecuteScalar();
               return b;       }
           catch (Exception )
           {           throw;
           }
           finally
           {
               con.Close();
           }
        }
       public static object GetOneData(string sql, List<SqlParameter> lt,CommandType type)
       {
           try
           {
               con.Open();
               SqlCommand com = new SqlCommand(sql, con);
               com.CommandType = type;
               if (lt != null)
               {
                   foreach (SqlParameter sp in lt)
                   {
                       com.Parameters.Add(sp);
                   }
               }
               object b = com.ExecuteScalar();
               return b;       }
           catch (Exception)
           {           throw;
           }
           finally
           {
               con.Close();
           }
       }
         //执行增加、删除、修改的语句
       public static int ExeSql(string sql, List<SqlParameter> lt, CommandType type)
       {
           
           try
           {
               con.Open();
               SqlCommand com = new SqlCommand(sql, con);
               com.CommandType = type;
               if (lt != null)
               {
                   foreach (SqlParameter sp in lt)
                   {
                       com.Parameters.Add(sp);
                   }
               }
               int i = com.ExecuteNonQuery();
               return i;
           }
           catch (Exception)
           {           throw;
           }
           finally
           {
               con.Close();
           }   }
         //执行查询语句,返回结果集
       public static SqlDataReader GetReader(string sql, List<SqlParameter> lt, CommandType type)
       {
           try
           {
               con.Open();
               SqlCommand com = new SqlCommand(sql, con);
               com.CommandType = type;
               if (lt != null)
               {
                   foreach (SqlParameter sp in lt)
                   {
                       com.Parameters.Add(sp);
                   }
               }
               SqlDataReader sdr = com.ExecuteReader(CommandBehavior.CloseConnection);//关闭DAtareader时,同时关闭数据连接
               return sdr;
           }
           catch (Exception)
           {           throw;
           }
          
       }
       //执行查询语句,返回dataset结果集
       public static DataSet GetDs(string sql, List<SqlParameter> lt, CommandType type)
       {
           try
           {
               con.Open();
               SqlCommand com = new SqlCommand(sql, con);
               com.CommandType = type;
               if (lt != null)
               {
                   foreach (SqlParameter sp in lt)
                   {
                       com.Parameters.Add(sp);
                   }
               }
               SqlDataAdapter sda = new SqlDataAdapter(com);
               DataSet ds = new DataSet();
               sda.Fill(ds);
              
               return ds;
           }
           catch (Exception)
           {           throw;
           }
           finally 
           {
               con.Close();
           }   }
    }
    各位大神帮我看看,有错么?
      

  4.   

    你的 SqlDataReader GetReader(....)这类方法接口,返回了一个非常危险的 DBDataReader而不保证它及时被执行 Close 操作,这种东西提取出来做为方法库,其实就是给自己找麻烦。最好的办法是把它删掉,然后在使用 SqlDataReader 的地方仍然手写(多写)那几行代码,这样才不易引来这种麻烦。
      

  5.   

     public static SqlDataReader GetReader(string sql, List<SqlParameter> lt, CommandType type)这个函数的返回,在调用完毕之后,没有关闭
    一个connection同时只能有一个command,一个command上同时只能有一个未关闭的datareader
    回答完毕