开发者

Is this SUM() query correct?

   string sql2 = "SELECT SUM(fy开发者_StackOverflowringsolje) FROM Informasjon WHERE fyringsolje='Kull: 2,42 kg';";
         SqlCommand myCommand2 = new SqlCommand(sql2, conn);
         kullTotaltLabel.Text = "" + (int)myCommand2.ExecuteScalar();

I don't see the text of the label changing, i am afraid the query cannot be correct. I want to make sure the query is correct!


It cant be right. In the same select statement you are comparing the fyringsolje field to Text so that suggest that the value is a text field and you cant apply sum on it


I doubt that that you want the ; at the end of the query... but just not seeing the label changing suggests that if something's going wrong, you're swallowing any exception that's being thrown.

Before you fix the query, fix the design so that when errors occur, you get to find out about them - otherwise you'll never be confident about what's happened.

I'd also change the final line to:

kullTotaltLabel.Text = myCommand2.ExecuteScalar().ToString();


I suspect that the SUM() on a alphanumeric column might be the culprit. I assume it is text column because the condition on the select.

Run it in debug mode, set a breakpoint in the second line and inspect the sql variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜