添加到标题的SQL查询?

问题描述:

在c#Windows窗体中:添加到标题的SQL查询?

我无法将sql查询结果作为文本添加到ToolStripMenuItem.Text中。

ToolStripMenuItem标题应该是公司+该公司的sql表中有多少订单,应该每x秒更新一次。

每隔5秒钟将查询结果添加到文本中。我的问题是,“增加”它。 前5秒后,它看起来OK“REXTON 1”但在5秒钟就会出现之后“REXTON 11”等等...

这里是我的代码:

//Rexton ordre klar til bestilling 
     SqlConnection con = new SqlConnection(@"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;Database=ha;Persist Security Info=false; UID='" + globalvariables.user + "' ; PWD='" + globalvariables.psw + "'"); 
        SqlCommand command = con.CreateCommand(); 


        command.CommandText = "SELECT COUNT(*) from bestillinger WHERE firma = @rexton and udlevering BETWEEN @date and @dateadd"; 
        command.Parameters.AddWithValue("@bernafon", "Bernafon"); 
        command.Parameters.AddWithValue("@gn_resound", "GN Resound"); 
        command.Parameters.AddWithValue("@oticon", "Oticon"); 
        command.Parameters.AddWithValue("@phonak", "Phonak"); 
        command.Parameters.AddWithValue("@rexton", "Rexton"); 
        command.Parameters.AddWithValue("@siemens", "Siemens"); 
        command.Parameters.AddWithValue("@widex", "Widex"); 

        con.Open(); 
        command.ExecuteNonQuery(); 
        string result = command.ExecuteScalar().ToString(); 

        con.Close(); 

        if (result != "0") 
        { 
         rextonToolStripMenuItem.Text = rextonToolStripMenuItem.Text + " " + result; 
         rextonToolStripMenuItem.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF1919"); 
        } 

那是因为你正在设置rextonToolStripMenuItem.Text to rextonToolStripMenuItem.Text + " " + result这是追加到以前的文本

要么设置文本空白,并将其设置再次或只是说

rextonToolStripMenuItem.Text = "rexton " + result 
+0

谢谢,它的工作原理! – user3888775 2014-09-22 15:26:25

+0

接受这个答案,如果它帮助你.. :)欢呼! – 2014-09-22 15:28:16