作者:Happy_Kelly尊荣 | 来源:互联网 | 2023-05-18 06:28
ImtryingtodosomethingIfeelisquitesimpleinthegrandschemeofthings,howeverImclearly
I'm trying to do something I feel is quite simple in the grand scheme of things, however I'm clearly missing something. What I have is a simple database named 'localcollection'. What I would like to do is sum up the dollar amount of a column named 'purprice', and setting it as the text of a label (label4). I've been finding variants of code throughout the last couple days that suggest different ways of achieving this. The majority of my digging suggest that using ExecuteScalar is what I want to do. The code that I've been fumbling with follows.
我正在尝试做一些事情,我觉得在宏伟的计划中很简单,但我显然遗漏了一些东西。我所拥有的是一个名为“localcollection”的简单数据库。我想要做的是总结名为'purprice'的列的美元金额,并将其设置为标签的文本(label4)。在过去的几天里,我一直在寻找代码的变体,提出了实现这一目标的不同方法。我的大部分挖掘建议使用ExecuteScalar是我想要做的。我一直在摸索的代码如下。
SqlCeConnection mycOnn= new SqlCeConnection(Properties.Settings.Default.localbotdbConnectionString);
myconn.Open();
{
string result = "select sum(purprice) from localcollection";
SqlCeCommand showresult = new SqlCeCommand(result, myconn);
label4.Text = showresult.ExecuteScalar().ToString();
myconn.Close();
}
Others suggest using the SqlCeReader. I'm impartial to either of them, as long as one of them works, and I am clearly missing something (fault of my own). The reader rendition that I was trying to make work is:
其他人建议使用SqlCeReader。我对他们中的任何一个都是公正的,只要其中一个有效,我显然错过了一些东西(我自己的错误)。我试图做的工作的读者演绎是:
SqlCeCommand cmd = new SqlCeCommand("select sum(purprice) from localcollection");
SqlCeDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
label4.Text = reader.GetString(0);
}
myconn.Close();
Constructive advice appreciated. Thank you
建设性建议表示赞赏。谢谢
6 个解决方案