我有一些有效的代码,但是将结果返回到文本框需要很长时间。输出可能需要20秒钟才能输出,而且我没有太多数据可以筛查。
我得到正确的输出,但是效果不好。
Global variable:string newLine = Environment.NewLine;
public List<string> AppointmentTypesForReport(int month)
{
List<string> appointments = new List<string>();
string CS = ConfigurationManager.ConnectionStrings["U04i5a"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(CS))
{
try
{
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT type FROM appointment WHERE MONTH(start) = @month";
cmd.Parameters.AddWithValue("@month", month);
cmd.ExecuteNonQuery();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
appointments.Add(reader["type"].ToString());
}
}
}
catch (Exception)
{
throw;
}
}
return appointments;
}
private void TypesByMonthRadioButton_CheckedChanged(object sender, EventArgs e)
{
ReportsTextBox.Clear();
//resultTextBox.Text = string.Empty;
var typesOutput = "";
for (int m = 1; m <= 6; m++)
{
List<string> list = AppointmentTypesForReport(m);
var j = from t in list
group t by t into g
let totalNumber = g.Count()
orderby totalNumber descending
select new { Returned = g.Key, Tally = totalNumber };
typesOutput += newLine + DateTimeFormatInfo.CurrentInfo.GetMonthName(m) + newLine;
//convert to lambda
foreach (var t in j)
{
typesOutput += "Type of appointment scheduled: " + t.Returned + " " + "-Appointments of this type: " + t.Tally + newLine;
}
}
ReportsTextBox.Text = typesOutput;
}
代码运行时,以下是文本框中的输出。
一月 预定的约会类型:xyz-此类型的约会:2
2月
三月
四月
五月 预定的约会类型:普通医生-此类型的约会:1
六月 预定的约会类型:普通医生-此类型的约会:1
答案 0 :(得分:1)
至少您应该使用StringBuilder
来构建结果字符串:
var typesOutput = new StringBuilder();
...
//typesOutput += newLine + DateTimeFormatInfo.CurrentInfo.GetMonthName(m) + newLine;
typesOutput.AppendLine();
typesOutput.AppendLine(DateTimeFormatInfo.CurrentInfo.GetMonthName(m));
...
//typesOutput += "Type of appointment scheduled: " + t.Returned + " " + "-Appointments of this type: " + t.Tally + newLine;
typesOutput.AppendFormat("Type of appointment scheduled: {0} - Appointments of this type: {1}\n", t.Returned, t.Tally);
...
ReportsTextBox.Text = typesOutput.ToString();