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

时间:2018-03-01 22:17:52

标签: c# database winforms real-time

通过从数据库中获取结果来更新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();
                        }
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分: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秒调用一次数据库,并使用数据库中表的最新值刷新标签文本

答案 1 :(得分: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;
    }