sql问题,如何添加日期时间选择器 [英] sql problem, how to add date time picker

查看:83
本文介绍了sql问题,如何添加日期时间选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



能帮助我吗,



Hi Experts,

Can you please help me with this,

sqL = "SELECT remittance_no, remit_date, messenger, item, item_value, rate.product, rate.product_value FROM remittance INNER JOIN rate ON rate.product=remittance.item where rate.ratecode = '" & txtRateCode.Text & "' And remittance.messenger = '" & txtMessenger.Text & "' order by remit_date asc"





i想要添加日期过滤器,我想为我的vb windows窗体使用两个datetimepicker。



提前谢谢你,



raz



i want to add date filter, i want to use two datetimepicker for my vb windows forms.

Thank you in advance,

raz

推荐答案

首先,您的代码存在 SQL注入 [ ^ ] - 永远不要将用户输入连接成这样的sql语句。



使用参数化查询 [ ^ ]而不是。



使用参数的一个优点s也不必担心日期和文本字段上的单引号。



您的代码可以更改如下
Firstly, your code is at risk of SQL Injection[^] - never concatenate user input into sql statements like this.

Use parameterised queries[^] instead.

One of the advantages of using parameters is also not having to worry about single quote marks on date and text fields.

Your code could be changed as follows
Dim sql As String = "SELECT remittance_no, remit_date, messenger, item, item_value, rate.product, rate.product_value "
sql += "FROM remittance INNER JOIN rate ON rate.product=remittance.item "
sql += "where rate.ratecode = @RateCode And remittance.messenger = @Messenger "
sql += "and remit_date between @Date1 and @Date2 "
sql += "order by remit_date asc"

然后假设您有类似

Dim command As SqlCommand = New SqlCommand()

以及关联的连接等(或者只是替换您的变量名称)对于命令,在下面的代码中。

and the associated connection etc (or just substitute your variable name for command in the code below.

command.Parameters.AddWithValue("@RateCode", txtRateCode.Text)
command.Parameters.AddWithValue("@Messenger", txtRateCode.Text)

command.Parameters.AddWithValue("@Date1", DateTimePicker1.Value)
command.Parameters.AddWithValue("@Date2", DateTimePicker2.Value)



请注意,使用DateTimePicker控件可以通过使用


Note that with DateTimePicker controls you can avoid the time element by using

DateTimePicker1.Value.Date

<来避免使用time元素BR />


这篇关于sql问题,如何添加日期时间选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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