我有问题,只读第一条记录,必须是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();
1条答案
按热度按时间weylhg0b1#
使用
while
而不是if
: