gpt4 book ai didi

sql - 更改日期时间值的格式

转载 作者:行者123 更新时间:2023-12-02 09:23:34 27 4
gpt4 key购买 nike

每个人似乎都讨厌 DateTime 值。

在我的项目中,我有一个 SQL SELECT 查询来查找数据库中与参数值匹配的结果。

我的代码是:

Dim where As String = "WHERE [Supp_Code] = @scode " & _
"AND [DateFrom] = @datfrom " & _
"AND [DateTo] = @datto " & _
"AND [Comm_Code] = @ccode " & _
"AND [Customer_Code] = @custcode "

Dim sql As String = "SELECT [Comm_Code], [AqYear] FROM [Acquisition Commission] "

sql &= where & " ORDER BY [AqYear] ASC"

Dim cmd As New OleDb.OleDbCommand(sql, con)
cmd.Parameters.AddWithValue("@scode", cmbSupp.Text.Trim)
cmd.Parameters.AddWithValue("@datfrom", datFrom.Value)
cmd.Parameters.AddWithValue("@datto", datTo.Value)
cmd.Parameters.AddWithValue("@ccode", txtCommCode.Text)
cmd.Parameters.AddWithValue("@custcode", cmbCustomerCode.Text)

Dim daYear As New OleDb.OleDbDataAdapter(cmd)
Dim dsYear As New DataSet
daYear.Fill(dsYear)

代码本身没有问题。问题是 DataSet 只有 0 行,因为 datFrom.ValueDateTime 格式(这是一个 DateTimePicker control) 是 'MM/dd/yyyy',但在数据库中它存储为 'dd/MM/yyyy'。

在将其用作参数之前将其转换为正确的数据库格式的最简单方法是什么?

编辑

使用下面的评论/答案,我已将代码调整为以下内容:

 Dim test As DateTime
Dim test2 As DateTime
test = ugDates.ActiveRow.Cells("DateFrom").Value
test = ugDates.ActiveRow.Cells("DateTo").Value

Try
Dim where As String = "WHERE [Supp_Code] = @scode " & _
"AND [DateFrom] = @datfrom " & _
"AND [DateTo] = @datto " & _
"AND [Comm_Code] = @ccode " & _
"AND [Customer_Code] = @custcode "

Dim sql As String = "SELECT DISTINCT [Comm_Code], [AqYear] FROM [Acquisition Commission] "

sql &= where & " ORDER BY [AqYear] ASC"

Dim cmd As New OleDb.OleDbCommand(sql, con)
cmd.Parameters.Add("@scode", OleDbType.VarChar).Value = cmbSupp.Text
cmd.Parameters.Add(New OleDbParameter("@datfrom", OleDbType.Date).Value = test)
cmd.Parameters.Add(New OleDbParameter("@datto", OleDbType.Date).Value = test2)
cmd.Parameters.Add("@ccode", OleDbType.VarChar).Value = txtCommCode.Text
cmd.Parameters.Add("@custcode", OleDbType.VarChar).Value = cmbCustomerCode.Text

但是,它在第​​二个参数上给了我以下错误:

The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not Boolean objects.

最佳答案

The issue is that the DataSet only ever has 0 rows, because the DateTime format of datFrom.Value (which is a DateTimePicker control) is 'MM/dd/yyyy', but in the database it is stored as 'dd/MM/yyyy'.

除非您将日期存储为字符串,否则这可能不是问题。日期是一种结构,而不是字符串,并且本质上没有格式。格式化是应用于字符串的东西。它类似于整数或小数等数字。如果该字段确实是一个字符串,您应该重新 Access 您的架构并相应地更新类型。

您很可能尝试将参数值或查询值中的日期与时间进行比较。

  • 使用DateValue函数来修剪存储数据库的时间
  • 在日期值上使用 .Date 仅比较起息日期

代码:

Dim where As String = "WHERE [Supp_Code] = @scode " & _
"AND DateValue([DateFrom]) = @datfrom " & _
"AND DateValue([DateTo]) = @datto " & _
"AND [Comm_Code] = @ccode " & _
"AND [Customer_Code] = @custcode "


// select either OleDbType.Date or OleDbType.DBDate

cmd.Parameters.Add(new OleDbParameter("@datfrom", OleDbType.Date) With {.Value = datFrom.Value.Date})
cmd.Parameters.Add(new OleDbParameter("@datto", OleDbType.Date) With {.Value = datTo.Value.Date})

另一个可能的问题是逻辑,为什么不在 DateFromDateTo 上进行介于或范围检查而不是相等检查?

"AND DateValue([DateFrom]) >= @datfrom " & _ 
"AND DateValue([DateTo]) <= @datto " & _

理想情况下,您可以在此处为 @datfrom@datto 使用相同的值。上面的查询将是具有跨传入日期参数​​的开始/结束日期的任何项目。

关于sql - 更改日期时间值的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39871634/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com