先有一sql有50W。怎么循环去5w,返回
    public IList<ProblemCodeRawdataInfo> GetProblemCodeRawdataList()
{
IList< ProblemCodeRawdataInfo> Obj=new List< ProblemCodeRawdataInfo>();
            ScheduleTaskMgr task = new ScheduleTaskMgr();
            string sqlStr = "";
            sqlStr += "select  create_date, 'TMSS' DataSource, 'TMSS' Server, country, product_type, ";
            sqlStr += "series, sub_series, model, problem_type, problem_type_desc, problem_code, problem_desc, ";
            sqlStr += "mail_id case_id, reply_user engineer, subject, problem, reply , reply_mail_id ,reply_flag ";
            sqlStr += "from TMSS_PROBLEM ";
            sqlStr += “  WHERE create_date >= '1/1/2007 12:00:00 AM' AND create_date <= '1/11/2008 12:00:00 AM' AND product_type = 'Motherboard' "
            sqlStr += " union all ";
            sqlStr += "select  a.create_date, 'eSupport' DataSource, ";
            sqlStr += "(case a.site_id when '1000' then 'Taiwan' when '3000' then 'Europe' when '5000' then 'America' when '7000' then 'China' when 'AU' then 'AU' else 'Other' end) Server, ";
            sqlStr += "a.country, a.tmss_l1_name product_type, a.tmss_l2_name series, a.tmss_l3_name sub_series, a.tmss_m_name model, ";
            sqlStr += "b.problem_type, b.problem_type_desc, b.problem_code, b.problem_desc, cast(a.case_no as varchar) case_id, ";
            sqlStr += "d.full_name engineer, b.subject, b.problem, c.solution reply , a.es_guid, b.problem_line_no ";
            sqlStr += "from es_header a ";
            sqlStr += "join es_problem b on a.es_guid = b.es_guid ";
            sqlStr += "left outer join es_solution c on b.es_guid = c.es_guid and b.problem_line_no = c.problem_line_no ";
            sqlStr += "left outer join user_v d on b.create_accid = d.cur_id ";
            sqlStr += "WHERE a.create_date >= '1/1/2007 12:00:00 AM' AND a.create_date <= '1/11/2008 12:00:00 AM'AND a.tmss_l1_name = 'Motherboard'";            SqlConnection conn = new SqlConnection(ScheduleTaskMgr.CONN_STRING_NON_DTC);
                    SqlCommand cmd = new SqlCommand();
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = CommandType.Text;
                   SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            int i = 1;
            int oneBatchSize = 50000;
            while (dr.Read())
            {
                Obj.Add(PopulateProblemCodeRawdataFromDr(dr));
                Console.WriteLine(i.ToString());
                i++;
            }
             conn.Close();
             dr.Close();
             dr.Dispose();
             return Obj;
     }
求高手,怎么在SqlDataReader  dr ,循环取5w

解决方案 »

  1.   

    while (dr.Read()) 
                { 
                    
                    Obj.Add(PopulateProblemCodeRawdataFromDr(dr)); 
                    Console.WriteLine(i.ToString()); 
                    i++; 
                    if(i>oneBatchSize )
                      break;
                } 
      

  2.   

    如果只需要50000,最好是sql中就只取50000. select top 50000 col1,col2,...
      

  3.   

    我是循环取5w,每5w一起,不是只取5w,先取5w在重50001开始取下面5w,以此类推
      

  4.   

    先查询一次,将所有查询结果放到一个临时表里,这一步是数据库做的应该会比较快,然后你再一次取出5w条,取出以后吧前5w条删掉,依次类推,比较笨,只能想到这个,
    SqlServer好像可以用系统方法来变相操作,你可以去google搜一下子
      

  5.   

    那你加个参数变成GetProblemCodeRawdataList(int startId, int pageNo){...}
      

  6.   


    public IList <ProblemCodeRawdataInfo> GetProblemCodeRawdataList(SqlDataReader reader, int count)
    {
    IList < ProblemCodeRawdataInfo> Obj=new List < ProblemCodeRawdataInfo>(); 
     if(count<=0)
        return Obj;
     int i = 1; 
                while (dr.Read()) 
                { 
                    if(i>count)
                       break;
                    Obj.Add(PopulateProblemCodeRawdataFromDr(dr)); 
                    Console.WriteLine(i.ToString()); 
                    i++; 
                } 
    return Obj;
    }
     SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
                int oneBatchSize = 50000;
                
                IList < ProblemCodeRawdataInfo> list1 = GetProblemCodeRawdataList(reader, oneBatchSize );
    IList < ProblemCodeRawdataInfo> list2 = GetProblemCodeRawdataList(reader, oneBatchSize );
    IList < ProblemCodeRawdataInfo> list3 = GetProblemCodeRawdataList(reader, oneBatchSize );
    //...
                conn.Close(); 
      

  7.   

    通过sql存储过程实现分页,再通过存储过程和游标实现查询
      

  8.   

    可以用SqlDataReader,每次读取一条我上次也遇到同样的问题
      

  9.   

    循环里面加个I,每循环一次I++ 一次,当第5W次的时候跳出就完了