将数据加载到winform中的流布局面板

Load data into flow layout panel in winforms

我有一个 UserControl,它有 3 个标签和 2 个图片框。我将数据库保存在 sql 服务器中并且有 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();
            }
        }));
    } 

对于这种类型的数据加载方案,您应该使用 async await。这意味着代码将在等待 return 请求时暂停,并且线程可以处理用户输入。

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