在C#中用sql选择求和

编程入门 行业动态 更新时间:2024-10-26 14:31:39
本文介绍了在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选择求和

本文发布于:2023-11-07 01:11:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565146.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中用   sql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!