将SQL数据绑定到WPF中的Label

时间:2016-02-02 22:38:51

标签: c# sql-server wpf

有没有办法将SQL数据绑定到WPF中的标签?

我有一个SQL数据库,总共有总结数据。我想将最后一列的数据显示给标签。因此,如果该列读取" 10"然后我需要标签来显示" 10"。

以下是我尝试过的代码:

C#:

SqlConnection con = new SqlConnection(myconnectioninfo);
SqlCommand scom = new SqlCommand();
scom.CommandText = "SELECT TOP 1 [TotalIncidents] FROM sixMonthReport ORDER BY [TotalIncidents] DESC"
Object temp = cmd.ExecuteScalar();
label.Text = temp.ToString();

大多数似乎都工作,直到我到达了标签部分,这不是WPF中的一个选项。

我无法在WPF /标签上找到任何有关任何帮助的信息。

1 个答案:

答案 0 :(得分:0)

label没有text property,但确实有content个,所以您可以尝试以下内容:

Object temp = null;
using (SqlConnection con = new SqlConnection(myconnectioninfo))
{
    con.Open();
    using (SqlCommand command = new SqlCommand("SELECT TOP 1 [TotalIncidents] FROM sixMonthReport ORDER BY [TotalIncidents] DESC", con))
    {
        temp = cmd.ExecuteScalar();
    }
}
label.Content = temp?.ToString();