命令文本,文本框C# [英] command text, text box C#

查看:107
本文介绍了命令文本,文本框C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cmd.CommandText = "Select * from Orders2 where ((month([OrderDate])>= '" + textBox1.Text + "') and (year([OrderDate])>= '" + txtyear1.Text + "')) AND ((month([OrderDate])<= '" + textBox2.Text + "') and (year([OrderDate])<= '" + txtyear2.Text + "'));";


我使用上面的命令根据日期在水晶报表中显示了销售额,但是当我输入这些报表时:

textBox1为"12",
txtyear1为"2010",

textBox2为"01",
和txtyear2设为"2011",
水晶报表未显示任何内容.
但是,当我输入以下内容时它会起作用:

textBox1为"11",
txtyear1为"2010",

textBox2为"11"或"12",
和txtyear2设为"2011"


I used the command above to show sales in crystal reports according to date, but when I input these:

textBox1 as "12",
txtyear1 as "2010",

textBox2 as "01",
and txtyear2 as "2011",
the crystal report didn''t show anything.
But, it works when I input:

textBox1 as "11",
txtyear1 as "2010",

textBox2 as "11" or "12",
and txtyear2 as "2011"

what could I do to improve the command without using parameterized query?

推荐答案

不,不是最好的方法.您应该使用参数化查询 [
No, not the best way. You should use parameterized query[^] with your command.


如果您不同的文本框仅包含Month和Year的值,那么这是可行的.如果文本框包含完整的日期,则还需要相应地转换为月或年,否则将无法进行比较.

或者,您也可以使用
DATEDIFF [
If your different Textbox contains only value of Month and Year only then It is feasible. If you Textbox contains full Date then you also need to convert then Either that in a month or Year accordingly, Otherwise comparison will not be done.

Or as an alternate you could use DATEDIFF[^] function which will give timespan in days,month or year between two dates and furthermore you could compare whether it is between valid range or not.

Please vote and Accept Answer if it Helped.


我认为更好的方法是:
1.创建具有诸如"DateTime?Value {get; set;}"之类的属性的自定义日历控件,并且不再使用两个文本框控件.
2.创建两个DateTime对象:1)startDate,2)endDate = endDate.AddDay(1).AddSeconds(-1).
3.将sql与参数化查询一起使用,例如从表中的*从@fromDate和@endDate之间的日期".
I think taht better way is:
1. Create custom calendar control, with have property like "DateTime? Value {get; set;}" and no more use two textbox controls.
2. Create two DateTime objects: 1) startDate, 2) endDate = endDate.AddDay(1).AddSeconds(-1).
3. Use sql with parameterized query like ''select * from table where date between @fromDate and @endDate''.


这篇关于命令文本,文本框C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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