我有一個 UserControl,它有 3 個標簽和 2 個圖片框。我將資料庫保存在 sql server 中并且有 380 條記錄。現在我有一個流布局面板。我想將每條記錄加載到我的用戶控制元件中。然后我使用流布局面板來添加這個控制元件。但是我的申請因此被延遲了。請幫我。
private void LoadMatch()
{
this.Invoke(new Action(() =>
{
using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-KBHC686\SQLEXPRESS;Initial Catalog=QLDB;Integrated Security=True"))
{
connection.Open();
string query = "Select T1.PIC,T1.CLBNAME,T2.PIC,T2.CLBNAME,TIME,SCORED1,SCORED2 from CLUB as T1, CLUB as T2, MATCH1 as M where M.CLB1 = T1.IDCLB and "
"M.CLB2 = T2.IDCLB order by DATE asc";
SqlDataAdapter ada = new SqlDataAdapter(query, connection);
DataTable dt = new DataTable();
ada.Fill(dt);
Match1 match;
foreach (DataRow row in dt.Rows)
{
match = new Match1();
match.lbClubHost.Text = row["CLBNAME"].ToString();
match.lbClubVisit.Text = row["CLBNAME1"].ToString();
string score1 = row["SCORED1"].ToString();
string score2 = row["SCORED2"].ToString();
byte[] img = (byte[])row["PIC"];
MemoryStream ms = new MemoryStream(img);
match.ptbClubHost.Image = Image.FromStream(ms);
byte[] img1 = (byte[])row["PIC1"];
MemoryStream ms1 = new MemoryStream(img1);
match.ptbClubVisit.Image = Image.FromStream(ms1);
if (!string.IsNullOrEmpty(score1) && !string.IsNullOrEmpty(score2))
{
match.lbScore.Text = score1 " - " score2;
}
else
{
match.lbScore.Text = "? - ?";
}
TimeSpan span = (TimeSpan)row["TIME"];
match.lbTime.Text = span.ToString(@"hh\:mm");
flpMatch.Controls.Add(match);
}
connection.Close();
}
}));
}
uj5u.com熱心網友回復:
您應該async await用于這種型別的資料加載場景。這意味著代碼將在等待請求回傳時暫停,并且執行緒可以處理用戶輸入。
使用的ExecuteReader性能可能比使用 DataAdapter 略高。
您還可以使用將控制元件批量添加到流程面板 AddRange
private async void LoadMatch()
{
var list = new List<Match1>();
try
{
const string query = @"
Select
T1.PIC,
T1.CLBNAME,
T2.PIC,
T2.CLBNAME,
TIME,
SCORED1,
SCORED2
from MATCH1 as M
JOIN CLUB as T1 ON M.CLB1 = T1.IDCLB
JOIN CLUB as T2 ON M.CLB2 = T2.IDCLB
order by DATE asc;
";
using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-KBHC686\SQLEXPRESS;Initial Catalog=QLDB;Integrated Security=True"))
using (var cmd = new SqlCommand(query, connection))
{
await connection.OpenAsync();
using (var reader = await cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
match = new Match1();
match.lbClubHost.Text = reader["CLBNAME"].ToString();
match.lbClubVisit.Text = reader["CLBNAME1"].ToString();
string score1 = reader["SCORED1"].ToString();
string score2 = reader["SCORED2"].ToString();
byte[] img = (byte[])reader["PIC"];
MemoryStream ms = new MemoryStream(img);
match.ptbClubHost.Image = Image.FromStream(ms);
byte[] img1 = (byte[])reader["PIC1"];
MemoryStream ms1 = new MemoryStream(img1);
match.ptbClubVisit.Image = Image.FromStream(ms1);
if (!string.IsNullOrEmpty(score1) && !string.IsNullOrEmpty(score2))
{
match.lbScore.Text = score1 " - " score2;
}
else
{
match.lbScore.Text = "? - ?";
}
match.lbTime.Text = (TimeSpan)reader["TIME"].ToString(@"hh\:mm");
list.Add(match);
}
}
}
flpMatch.Controls.AddRange(list);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/379701.html
下一篇:通過名稱作為字串訪問物件c#
