gpt4 book ai didi

c# - 将 Mysql 数据填充到 asp 日历加载时间太长

转载 作者:行者123 更新时间:2023-11-29 06:32:26 26 4
gpt4 key购买 nike

我正在为员工创建休假日历,为此我使用数据集将一些数据填充到日历中,但加载数据需要很长时间。

我使用多个 MySqlDataReader连接 从 MySql 表中读取日历表每一行的数据。也许使用多个连接和读取器可能是速度变慢的原因,但我不确定。下面是我用来填充数据的代码。

class Sample
{
public DateTime Date { get; set; }
public string SlotAvailable { get; set; }
public string Pending { get; set; }
public string HeadCount { get; set; }
}
DateTime firstDate { get; set; }
DateTime lastDate { get; set; }
List<Sample> samples = new List<Sample>();
protected DataSet dsleaveplanner;
protected void FillLeaveplannerDataset()
{
cal2.VisibleDate = cal2.TodaysDate;
DateTime firstDate = new DateTime(cal2.VisibleDate.Year, cal2.VisibleDate.Month, 1).AddDays(-6);
DateTime lastDate = new DateTime(cal2.VisibleDate.Date.AddMonths(1).Year, cal2.VisibleDate.Date.AddMonths(1).Month, 1).AddDays(7);
dsleaveplanner = GetCurrentMonthData(firstDate, lastDate);
}
protected DateTime GetFirstDayOfNextMonth()
{
int monthNumber, yearNumber;
if (cal2.VisibleDate.Month == 12)
{
monthNumber = 1;
yearNumber = cal2.VisibleDate.Year + 1;
}
else
{
monthNumber = cal2.VisibleDate.Month + 1;
yearNumber = cal2.VisibleDate.Year;
}
DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
return lastDate;
}
protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate)
{
string site = lblsite.Text;
string skill = lblskill.Text;
string shift = lblshift.Text;
DataSet dsMonth = new DataSet();
string MyConString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
MySqlConnection con = new MySqlConnection(MyConString);
string caldate = "Select * From setshrinkage Where date >= @firstDate And date <= @lastDate And site=@site And skill=@skill And shift=@shift Group By date";
MySqlCommand cmd = new MySqlCommand(caldate, con);
cmd.Parameters.AddWithValue("@firstDate", firstDate);
cmd.Parameters.AddWithValue("@lastDate", lastDate);
cmd.Parameters.AddWithValue("@site", site);
cmd.Parameters.AddWithValue("@skill", skill);
cmd.Parameters.AddWithValue("@shift", shift);
MySqlDataAdapter mysqlDataAdapter = new MySqlDataAdapter(cmd);
try
{
mysqlDataAdapter.Fill(dsMonth);
con.Close();
}

catch { }
return dsMonth;

}
public void caldisp(DayRenderEventArgs e)
{
Environment.NewLine.ToString();
e.Cell.ForeColor = System.Drawing.Color.Red;
e.Cell.Font.Size = 9;
e.Cell.Controls.Add(new LiteralControl("<p></p>Slot available:"));
e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().SlotAvailable.ToString()));
e.Cell.Controls.Add(new LiteralControl("<p></p>Pending:"));
e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().Pending.ToString()));
}


protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
DateTime nextDate;
//e.Day.IsSelectable = false;
if (dsleaveplanner != null)
{
foreach (DataRow dr in dsleaveplanner.Tables[0].Rows)
{
nextDate = (DateTime)dr["date"];
var hcount = (dr["headCount"].ToString());
Int32 hcount1 = Convert.ToInt32(hcount);
string MyConString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
MySqlConnection conn = new MySqlConnection(MyConString);
string cntdate = "SELECT COUNT(date) FROM approved WHERE date = @date And site=@site And skill=@skill And shift=@shift And status=@status";
string cntdate2 = "SELECT COUNT(date) FROM approved WHERE date = @date And site=@site And skill=@skill And shift=@shift And status=@status";
MySqlCommand cmd2 = new MySqlCommand(cntdate, conn);
MySqlCommand cmd3 = new MySqlCommand(cntdate2, conn);
cmd2.Parameters.AddWithValue("@date", nextDate);
cmd2.Parameters.AddWithValue("@site", lblsite.Text);
cmd2.Parameters.AddWithValue("@skill", lblskill.Text);
cmd2.Parameters.AddWithValue("@shift", lblshift.Text);
cmd2.Parameters.AddWithValue("@status", "auto-approved");
cmd3.Parameters.AddWithValue("@date", nextDate);
cmd3.Parameters.AddWithValue("@site", lblsite.Text);
cmd3.Parameters.AddWithValue("@skill", lblskill.Text);
cmd3.Parameters.AddWithValue("@shift", lblshift.Text);
cmd3.Parameters.AddWithValue("@status", "pending");
string chklog = "SELECT date FROM approved WHERE date = @date And agentlogin=@login And status=@stat";
MySqlCommand cmd1 = new MySqlCommand(chklog, conn);
cmd1.Parameters.AddWithValue("@date", nextDate);
cmd1.Parameters.AddWithValue("@login", Label1.Text);
cmd1.Parameters.AddWithValue("@stat", "auto-approved");
conn.Open();
string count = cmd2.ExecuteScalar().ToString();
string count2 = cmd3.ExecuteScalar().ToString();
var slot2 = Convert.ToInt32(count);
Int32 slot3 = hcount1 - slot2;
string slot4 = slot3.ToString();
MySqlDataReader dr1 = cmd1.ExecuteReader();
MySqlConnection con = new MySqlConnection(MyConString);
string chklog1 = "SELECT date FROM approved WHERE date = @date And agentlogin=@login And status=@stat";
MySqlCommand cmd4 = new MySqlCommand(chklog1, con);
cmd4.Parameters.AddWithValue("@date", nextDate);
cmd4.Parameters.AddWithValue("@login", Label1.Text);
cmd4.Parameters.AddWithValue("@stat", "pending");
con.Open();
MySqlDataReader dr2 = cmd4.ExecuteReader();
MySqlConnection con2 = new MySqlConnection(MyConString);
string chklog2 = "SELECT date FROM approved WHERE date = @date And agentlogin=@login And status=@stat";
MySqlCommand cmd5 = new MySqlCommand(chklog2, con2);
cmd5.Parameters.AddWithValue("@date", nextDate);
cmd5.Parameters.AddWithValue("@login", Label1.Text);
cmd5.Parameters.AddWithValue("@stat", "rejected");
con2.Open();
MySqlDataReader dr3 = cmd5.ExecuteReader();
MySqlConnection con3 = new MySqlConnection(MyConString);
string chklog3 = "SELECT date FROM approved WHERE date = @date And agentlogin=@login And status=@stat";
MySqlCommand cmd6 = new MySqlCommand(chklog3, con3);
cmd6.Parameters.AddWithValue("@date", nextDate);
cmd6.Parameters.AddWithValue("@login", Label1.Text);
cmd6.Parameters.AddWithValue("@stat", "agent-withdrawn");
con3.Open();
MySqlDataReader dr4= cmd6.ExecuteReader();
if (nextDate == e.Day.Date)
{
if (dr1.HasRows)
{
e.Cell.BackColor = System.Drawing.Color.LightGreen;
}
else if (dr2.HasRows)
{
e.Cell.BackColor = System.Drawing.Color.Gold;
}
else if (dr3.HasRows)
{
e.Cell.BackColor = System.Drawing.Color.Tomato;
}
else if (dr4.HasRows)
{
e.Cell.BackColor = System.Drawing.Color.DarkTurquoise;
}
}
conn.Close();
con.Close();
con2.Close();
con3.Close();
samples.Add(new Sample { Date = nextDate, SlotAvailable = slot4, Pending = count2 });
}
if (samples.Any(x => x.Date == e.Day.Date))
{
string weekoff = lblweekoff.Text;
List<string> offday = (lblweekoff.Text).Split(',').ToList();
if (offday.Contains(e.Day.Date.ToString("ddd")))
{
e.Cell.Font.Size = 9;
e.Cell.Controls.Add(new LiteralControl("<p>Week-Off </p>"));
}
else
{
caldisp(e);
}
}
else
{
e.Cell.ForeColor = System.Drawing.Color.Red;
e.Cell.Font.Size = 9;
e.Cell.Controls.Add(new LiteralControl("<p>Target not set! </p>"));
}
}
}

如何才能加快此过程?感谢任何帮助,提前致谢!

最佳答案

foreach 语句中有 6 个 SQL 查询,如果 dsleaveplanner 中有 10 行,程序将执行 60 个 SQL 查询。这个数量的 SQL 查询会对性能产生负面影响。
尝试检索 foreach 语句之前的所有数据并将库存数据放入列表(内存)中,然后在 foreach 中使用它

关于c# - 将 Mysql 数据填充到 asp 日历加载时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55569672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com