在winforms中将数据加载到流布局面板中



我有一个UserControl,它有3个标签和2个图片框。我把数据库保存在sql server中,有380条记录。现在我有了一个流程布局面板。我想将每个记录加载到我的用户控件中。然后我使用流布局面板来添加这个控件。但是我的申请被推迟了。请帮帮我。

private void LoadMatch()
{
this.Invoke(new Action(() =>
{
using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-KBHC686SQLEXPRESS;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();
}
}));
} 

对于这种类型的数据加载场景,应该使用asyncawait。这意味着在等待请求返回时,代码将被挂起,线程可以处理用户输入。

使用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-KBHC686SQLEXPRESS;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);
}
}

最新更新