简体   繁体   English

DateTimePicker中的Visual Basic日期在SQL中不起作用

[英]Visual Basic date from DateTimePicker doesn't work in SQL

I just need to ask about DateTimePicker since when I run the query below, I get the correct data in between the month and days but the year is wrong. 我只需要问一下DateTimePicker,因为当我运行下面的查询时,我在月份和几天之间获得了正确的数据,但是年份是错误的。 DATEOFTRANSACTION has varchar as data type. DATEOFTRANSACTION具有varchar作为数据类型。

Example scenario 示例场景

DateTimePicker1 = 06/10/2017 DateTimePicker1 = 06/10/2017

DateTimePicker2 = 10/22/2017 DateTimePicker2 = 10/22/2017

Database has: 数据库具有:

06/10/2017 2017年6月10日
08/02/2017 2017年8月2日
12/15/2017 2017年12月15日
08/02/2018 2018年8月2日

After running the query my display is: 运行查询后,我的显示是:

06/10/2017 2017年6月10日
08/02/2017 2017年8月2日
08/02/2018 2018年8月2日

Query String to get dates in between Picker1 and Picker2 查询字符串以获取介于Picker1和Picker2之间的日期

Dim rsa As New OracleCommand(
    "SELECT * FROM DBOR WHERE DATEOFTRANSACTION BETWEEN '" 
    & Format(DateTimePicker1.Value, "MM/dd/yyyy").ToString & "' AND '" 
    & Format(DateTimePicker2.Value, "MM/dd/yyyy").ToString & "'", con)

My insert query is this 我的插入查询是这个

Dim rs As New OracleCommand(
    "Insert into DBOR (OR_NUMBER,FIRSTNAME,MIDDLENAME,LASTNAME,DATEOFTRANSACTION,TIMEOFTRANSACTION, PRICE, CASHIER) values ('" 
    & RECEIPTform.Label1.Text & "', '" 
    & ADDCUSTOMERform.FirstName.Text & "','" 
    & ADDCUSTOMERform.MiddleName.Text & "','" 
    & ADDCUSTOMERform.LastName.Text & "' ,'" 
    & Format(Date.Now(), "MM/dd/yyyy") & "' , '" 
    & TimeOfDay.ToString("h:mm:ss tt") & "', '" 
    & TextBox1.Text & "', '" 
    & LOGINform.username.Text & "')", con)

First, as commented before, you need to use parameters instead of concatenating strings values. 首先,如前所述,您需要使用参数而不是串联字符串值。 OracleCommand.Parameters Property ie: OracleCommand.Parameters属性,即:

OracleCommand oraCommand = new OracleCommand("SELECT fullname FROM sup_sys.user_profile
                           WHERE domain_user_name = :userName", db);
oraCommand.Parameters.Add(new OracleParameter("userName", domainUser));

Second, dates stored in DB as string in this format: 其次,日期以字符串格式存储在数据库中:

YEAR(YYYY/YY)[optional separatos]Month(MM)[optional separatos]DAY(DD)

alphabetical and chronological order match, other formats, not. 字母和时间顺序匹配,其他格式则不然。

You still have to deal with the problem where your dates in DB are stored as MM/DD/YYYY or DD/MM/YYYY ( I can't tell with data sample provided) 您仍然必须解决数据库中的日期存储为MM/DD/YYYYDD/MM/YYYY (我无法通过提供的数据样本来判断)

So, it is not enough that you convert your DateTimePicker in the formal MM/DD/YYYY because this string is sorted alphabetically and this does not match the chonologycal order you need. 因此,仅用正式的MM/DD/YYYY转换DateTimePicker是不够的,因为此字符串是按字母顺序排序的,并且与所需的排序顺序不匹配。

You need to convert char field in dates or in string format YYYY/MM/DD 您需要以日期或字符串格式YYYY/MM/DD转换char字段

In order to do this, you need something like this 为了做到这一点,你需要这样的东西

TO_DATE(DATEOFTRANSACTION,'MM/DD/YYYY')

It will look like this: 它看起来像这样:

OracleCommand rsa = new OracleCommand("SELECT * FROM DBOR 
                WHERE TO_DATE(DATEOFTRANSACTION,'MM/DD/YYYY') 
                BETWEEN iniDate = :iniDate AND endDate= :endDate", con);
rsa.Parameters.Add(new OracleParameter("iniDate", DateTimePicker1.Value));
rsa.Parameters.Add(new OracleParameter("endDate", DateTimePicker2.Value));

EDIT: I just realized it is used MM/DD/YYYY format, but I'll leave the previous comment for others to see probable problems. 编辑:我刚刚意识到它使用MM/DD/YYYY格式,但我将前面的评论留给其他人看可能的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM