在C#中用sql选择求和 [英] select sum query by sql in C#

查看:135
本文介绍了在C#中用sql选择求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将GlassesPrice列中的所有值相加并在消息框中显示。

 double income = 0; 
DataTable dt = new DataTable();
string date = toolStripComboBox3.Text +/+ toolStripComboBox2.Text +/+ toolStripComboBox1.Text;
string strconn = @Data Source = C:\DB\opticsinfo.sdf;
SqlCeConnection conn = new SqlCeConnection(strconn);
conn.Open();
SqlCeCommand cmd = new SqlCeCommand(选择SUM(GlassesPrice)作为EyeGlasses的收入,其中CheckinDate =+ date +;,conn);
SqlCeDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
income = Convert.ToDouble(dr [0]);
}
conn.Close();
MessageBox.Show(income.ToString());

 





但我在下划线中有例外

数据转换失败。[OLE DB状态值(如果已知)= 2] 



有什么问题?

解决方案

请你查一下sqlquery,看看是否能获得数据基于sqlserver中的上述查询



如果查询正确,您可以更改下面的查询并运行,看看你得到的结果



 SqlCeCommand cmd =  new  SqlCeCommand( 选择SUM(GlassesPrice)作为EyeGlasses的收入,其中CheckinDate =' + date +  ',conn); 





如果您有任何问题...请询问


i want to sum all values in column "GlassesPrice" and show it in message box.

double income = 0;
            DataTable dt = new DataTable();
            string date = toolStripComboBox3.Text +"/"+ toolStripComboBox2.Text +"/"+ toolStripComboBox1.Text;
            string strconn = @"Data Source=C:\DB\opticsinfo.sdf";
            SqlCeConnection conn = new SqlCeConnection(strconn);
            conn.Open();
            SqlCeCommand cmd = new SqlCeCommand("select SUM(GlassesPrice) as income from EyeGlasses where CheckinDate=" + date + ";", conn);
            SqlCeDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                income = Convert.ToDouble(dr[0]);
            }
            conn.Close();
            MessageBox.Show(income.ToString());



but i have exception in the underline line
"

Data conversion failed. [ OLE DB status value (if known) = 2 ]

"
what is the problem??

解决方案

can you please check the sqlquery ,whether you are able to get the data based on above query in sqlserver

If query is correct can you change the query as below and run and see what result you are getting

SqlCeCommand cmd = new SqlCeCommand("select SUM(GlassesPrice) as income from EyeGlasses where CheckinDate='" + date +"'", conn);



If you have any issue ...please ask


这篇关于在C#中用sql选择求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆