根据开始日期和结束日期进行添加. [英] Addion on the bases of start date and end date.

查看:231
本文介绍了根据开始日期和结束日期进行添加.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据开始日期和结束日期来过滤数据..并且我正在基于该过滤器来执行价格的加法(总和)..

我已经编写了测试代码..但它不适用于开始日期和结束日期..

忽略 concentination 串联(SQL注入),我只是在将其实现到我的项目上之前编写了此代码..

这是测试代码..为了使该代码正常工作,我应该进行哪些修改.

这是代码..

i am trying to filter the the data on the bases of start date and end date.. and on the bases of that filter i am performing addition(Sum) of price..

i have written the test code.. but its not working with start date and end date..

ignore concentination concatenation (Sql Injection) i just wrote this before implementing it on my project..

here is the test code.. what should i amend to make that code work.

here is the Code..

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection MAconn = new SqlConnection();
        MAconn.ConnectionString = connectionString;
        MAconn.Open();
        
        SqlCommand MAcmd = MAconn.CreateCommand();

        MAcmd.CommandText = "select Sum(price) from sale where startdate='"+TextBox1.Text+"' and startdate='"+TextBox2.Text+"'";
        lblsale.Text = System.Convert.ToString(MAcmd.ExecuteScalar());

        
        MAconn.Close();

    }

推荐答案

毫无疑问,代码是完美的.但是您将StartDate和EndDate作为字符串.但是,如果您在sql Server中看到,则可能已将其dataType设置为DateTime.因此您必须将此字符串转换为dateTime,然后将其传递给Query.而不是使用文本框,我更喜欢使用DateTimePicker.
No Doubt the code is perfect. but you are taking StartDate and EndDate as string. but if you see in sql Server you may have set its dataType to DateTime. so you have to convert this string to dateTime and then pass it in Query. instead of using textbox, i would prefer to use DateTimePicker.


MAcmd.CommandText = "select Sum(price) from sale where startdate='"+TextBox1.Text+"' and startdate='"+TextBox2.Text+"'";


startdate不能等于两个值,除非它们是相同的字符串.这就是您所期望的吗?

我还谨防使用文本字符串进行日期比较,您应该始终使用DateTime值,因为它们不会被误解. "10/12/2011"形式的字符串是不明确的;是12月10日还是10月12日?


startdate cannot be equal to both values, unless they are the same string; is that what you expect?

I would also guard against using text strings for date comparisons, you should always use DateTime values as they cannot be misinterpreted. Strings of the form "10/12/2011" are ambiguous; is that 10th of December or October 12th?


这篇关于根据开始日期和结束日期进行添加.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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