我如何将Sql Sum标记为C# [英] How Do I Sql Sum Get To Label C#
问题描述
亲爱的朋友们,
我需要在sql sum下打印到c#windows form应用程序中。这是我正在使用的代码。但是我收到了一个错误。
SQL查询为休闲。这个工作正在工作
SELECT SUM(COUNT) FROM Count_EPF WHERE date = ' 2015年6月17日'>
转换从字符串转换datetime时失败
听到我的代码
string query = SELECT SUM( COUNT)FROM Count_EPF WHERE date ='@ C';
使用(SqlCommand cmd = new SqlCommand(query,conn))
{
cmd.Parameters.AddWithValue( @ C,dateTimePicker1.Text );
cmd.ExecuteNonQuery();
object result = cmd.ExecuteScalar();
txtView.Text = Convert.ToString(result);
删除引号:
string query = SELECT SUM(COUNT)FROM Count_EPF WHERE date ='@ C ';
成为:
字符串查询= SELECT SUM(COUNT)FROM Count_EPF WHERE date = @C;
否则参数名称在SQL字符串中,不会被视为参数完全。
哦,不要传递字符串 - 直接传递DateTime,值:
cmd.Parameters.AddWithValue( @ C,dateTimePicker1.Value);
这样,SQL就不必将字符串解释为具有可能导致的所有可能误解的日期。
[/编辑]
Dear friends,
I need to print below sql sum into c# windows form application. here is the code i was using for this. but i'm getting an error.
SQL Query as fallows. this one is working
SELECT SUM (COUNT) FROM Count_EPF WHERE date = '6/17/2015'>
conversion failed when converting datetime from character string
hear is my code
string query = "SELECT SUM (COUNT) FROM Count_EPF WHERE date = '@C' ";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@C", dateTimePicker1.Text);
cmd.ExecuteNonQuery();
object result = cmd.ExecuteScalar();
txtView.Text = Convert.ToString(result);
Remove the quotes:
string query = "SELECT SUM (COUNT) FROM Count_EPF WHERE date = '@C' ";Becomes:
string query = "SELECT SUM (COUNT) FROM Count_EPF WHERE date = @C ";Otherwise the parameter name is within an SQL string and is not treated as a parameter at all.
[edit]
Oh, and don't pass the string - pass the DateTime, value directly:
cmd.Parameters.AddWithValue("@C", dateTimePicker1.Value);
That way, SQL doesn't have to interpret the string into a date with all the possible misinterpretations that can cause.
[/edit]
这篇关于我如何将Sql Sum标记为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!