C#从数据库实时更新标签

问题描述 投票:0回答:2

通过从数据库中获取结果来更新winforms上的标签的好方法是什么?因此,所有用户都可以实时获取更新,而不是启动winform以获取更新。

    public partial class labelControl : UserControl
{
    public labelControl()
    {
        InitializeComponent();
        updateLabel();
    }

    private void updateLabel()
    {
        using (Database.sqlConnection = new SqlConnection(Database.connectionString))
        {
            Database.sqlConnection.Open();
            string selectQuery = "SELECT * FROM Information";

            using (SqlCommand sqlCommand = new SqlCommand(selectQuery, Database.sqlConnection))
            {
                using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
                {
                    if (sqlReader.HasRows)
                    {
                        while (sqlReader.Read())
                        {
                            label1.Text = sqlReader["Text"].toString();
                        }
                    }
                }
            }
        }
    }
}
c# database winforms real-time
2个回答
1
投票
public partial class labelControl : UserControl
{
    private const short interval = 5;
    private Timer timer;
    public labelControl()
    {
        InitializeComponent();
        timer = new Timer()
        timer.Interval = interval * 1000; //time to refresh the label in seconds
        timer.Tick += updateLabel;
        timer.Start();
    }

    private void updateLabel()
    {  
        try
        {
        using (Database.sqlConnection = new SqlConnection(Database.connectionString))
        {
            Database.sqlConnection.Open();
            string selectQuery = "SELECT Text FROM Information WHERE ID = (SELECT MAX(ID) FROM Information)"; // you probably want the latest value

            using (SqlCommand sqlCommand = new SqlCommand(selectQuery, Database.sqlConnection))
            {
                using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
                {
                    if (sqlReader.HasRows)
                    {
                        while (sqlReader.Read())
                        {
                            label1.Text = sqlReader["Text"].ToString();
                        }
                    }
                }
            }
        }
        }
        catch()
        {
            //do nothing
        }
    }
}

使用此解决方案,您每隔5秒调用一次数据库,并使用数据库中表的最新值刷新标签文本


0
投票

试试这个,在后台添加一个Task运行,并在刷新间隔时使用sleep线程。

    public labelControl()
    {
        InitializeComponent();
        if (!this.DesignMode)
        {
            RefreshLabel();
        }
    }

    private async void RefreshLabel()
    {
        await Task.Run(() => {
            while (true)
            {
                try
                {
                    using (Database.sqlConnection = new SqlConnection(Database.connectionString))
                    {
                        Database.sqlConnection.Open();
                        string selectQuery = "SELECT Text FROM Information WHERE ID = (SELECT MAX(ID) FROM Information)"; // you probably want the latest value

                        using (SqlCommand sqlCommand = new SqlCommand(selectQuery, Database.sqlConnection))
                        {
                            using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
                            {
                                if (sqlReader.HasRows)
                                {
                                    while (sqlReader.Read())
                                    {
                                        ChangeLabelText(sqlReader["Text"].ToString());
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                System.Threading.Thread.Sleep(500); // time to sleep thread
            }
        });
    }

    private void ChangeLabelText(string value)
    {
        if (label1.InvokeRequired)
        {
            label1.Invoke(new Action<string>(ChangeLabelText), value);
            return;
        }
        label1.Text = value;
    }
© www.soinside.com 2019 - 2024. All rights reserved.