【问题标题】:manipulating the date and time from a datetime sql server type in vb.net从 vb.net 中的 datetime sql server 类型操作日期和时间
【发布时间】:2011-08-25 00:59:36
【问题描述】:

我正在尝试使用 asp.net 创建一个拍卖网站。我的拍卖开始日期和时间以及结束日期和时间作为日期时间类型存储在我的 sql server 2008 r2 数据库中。我正在使用 vb.net。我正在尝试创建一个选择语句,我可以在其中查询相对于当前日期和时间的日期和时间部分(以便仅显示当前正在进行的拍卖)。我可以使用 Date.Now 函数成功查询日期部分,但是时间给我带来了麻烦。 DateAndTime.Now 不起作用。我什至尝试在数据库中分离时间和日期,但仍然无法得到有效的查询。

这是我尝试过的两个代码示例:

Dim Connection As SqlConnection = New SqlConnection(...)
    Dim Command As SqlCommand = New SqlCommand()
    Dim _Reader As SqlDataReader
    Command.Connection = Connection
    Command.CommandText = "Select ... from Auctions where Start_Date_Time > " & DateAndTime.Now & " And End_Date_Time > " & DateAndTime.Now

其中 Start_Date_Time 和 End_Date_Time 是 DateTime 类型。

还有:

Dim Connection As SqlConnection = New SqlConnection(...)
    Dim Command As SqlCommand = New SqlCommand()
    Dim _Reader As SqlDataReader
    Command.Connection = Connection
    Command.CommandText = "Select ... from Auctions where Start_Date >= " & Date.Today & "Start_Time <= " & DateAndTime.Now.ToLongTimeString & " And End_Date >= " & Date.Today & " And End_Time <= " & DateAndTime.Now.ToLongTimeString

其中 Start_Date 和 End_Date 属于 Date 类型,而 Start_Time 和 End_Time 属于 Time(7) 类型。

感谢任何建议, 提前致谢

【问题讨论】:

  • 使用参数化查询。不要将日期和时间作为字符串传递。您也可以考虑使用 TSQL getdate()dateadd 函数,而不是根据当前时间传递参数。
  • @Martin 关于使用 T-SQL 值的唯一警告是服务器可能位于不同的时区。我们曾经遇到过一个问题,英国数据中心的一个应用程序写入弗吉尼亚州的数据库,没有人同意应用程序应该显示什么或应该存储在数据库中的内容。从那时起,我一直努力在 UTC 中部署 SQL 和应用服务器,但如果应用需要应用服务器的日期/时间...

标签: asp.net vb.net sql-server-2008-r2


【解决方案1】:

您是否尝试过使用DateAndTime.TimeString Property

TimeString always returns the system time as "HH:mm:ss", which is a 24-hour format. 因此,根据 DateTime 的时间部分在数据库中的存储方式,您可能需要进行一些格式化。

Dim Connection As SqlConnection = New SqlConnection(...) 
Dim Command As SqlCommand = New SqlCommand()
Dim _Reader As SqlDataReader
Command.Connection = Connection          
Command.CommandText = "Select ... from Auctions where Start_Date >= " & Date.Today & "Start_Time <= " & DateAndTime.TimeString & " And End_Date >= " & Date.Today & " And End_Time <= " & DateAndTime.TimeString      

【讨论】:

    【解决方案2】:
    dim myDateTime as Datetime = Now()
    
    ...
    
    Select ... from Auctions where Start_Date_Time > " & myDateTime.ToShortTimeString() & ...
    

    http://msdn.microsoft.com/en-us/library/system.datetime.toshorttimestring.aspx

    【讨论】:

    • OP 使用的是DateAndTime,而不是日期时间。
    • @Tim -- 实际上,他正在使用它以及时间。他可以很容易地使用 DateTime。我认为他并不真正关心变量类型是什么。但我会更新我的答案以澄清 - 谢谢。
    【解决方案3】:

    感谢大家的建议。这是我的一个简单错误。我需要做的就是将 DateAndTime.Now 放在引号中。

    所以这段代码有效:

    Dim Connection As SqlConnection = New SqlConnection(...)
    Dim Command As SqlCommand = New SqlCommand()
    Dim _Reader As SqlDataReader
    Command.Connection = Connection
    Command.CommandText = "Select ... from Auctions where Start_Date_Time <= '" & DateAndTime.Now & "' And End_Date_Time >= '" & DateAndTime.Now & "'"
    

    【讨论】:

    • 不要使用字符串连接。不仅您的性能会受到影响,而且您还会面临 SQL 注入攻击。请改用参数化 SQL。
    • @Jim 感谢您的建议,但这个网站永远不会上线。这只是为了演示目的。
    猜你喜欢
    • 1970-01-01
    • 2015-10-02
    • 2017-01-10
    • 1970-01-01
    • 2011-08-08
    • 2013-12-17
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多