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

问题描述

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

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

忽略串联(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 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();

    }

推荐答案


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?


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

07-23 15:22
查看更多