如何通过mysqlreader c读取多个请求#

flmtquvp  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(329)

我有问题,只读第一条记录,必须是5条记录从数据库。帮我解决这个问题。我有三个要求在一个命令,我认为它的工作对我来说不正常。
db中的记录截图

结果

var model = new List<WorkPeriod1>();
        MySqlCommand cmd2 = new MySqlCommand("SELECT DISTINCT target_date FROM work_period w, aspnetusers u WHERE u.Id=w.id_manager AND u.UserType=0 AND u.Id_num<>0 AND u.IsActive = TRUE AND w.target_date >= DATE_SUB('2018-04-21',INTERVAL 7 DAY);" +
            "SELECT  w.start ,w.finish, WEEKDAY(w.target_date) dayssa FROM aspnetusers u , work_period w WHERE u.Id=w.id_manager AND u.UserType=0 AND u.Id_num<>0 AND u.IsActive = TRUE AND target_date >= DATE_SUB('2018-04-21',INTERVAL 7 DAY);" +
            "SELECT DISTINCT u.ContactName ,w.description FROM aspnetusers u , work_period w WHERE u.Id=w.id_manager AND u.UserType=0 AND u.Id_num<>0 AND u.IsActive = TRUE AND w.target_date >= DATE_SUB('2018-04-21',INTERVAL 7 DAY);", conn);

        conn.Open();
        using (conn)
        {

            MySqlDataReader reader = cmd2.ExecuteReader();
            while (reader.HasRows)
            //for (int i = 0; i < reader.FieldCount; i++)
            {
                var workperiod = new WorkPeriod1();
                if (reader.Read())
                {
                    workperiod.Rtarget_date = reader.GetDateTime(0);              
                }

                reader.NextResult();
                if (reader.Read())
                {
                    workperiod.RStart = Convert.ToDateTime(reader.GetString(0));
                    workperiod.RFinish = Convert.ToDateTime(reader.GetString(1));
                    workperiod.DWA = Convert.ToInt32(reader.GetInt32(2));

                }
                reader.NextResult();
                if (reader.Read())
                {
                    workperiod.RDescr = reader.GetString(1);
                    workperiod.RContactName = reader.GetString(0);        
                }
                reader.NextResult();
                model.Add(workperiod);
            }

            reader.Close();
weylhg0b

weylhg0b1#

使用 while 而不是 if :

if (reader.HasRows)
{
   while (reader.Read())
   {
      .........

相关问题