【问题标题】:Convert a datetime in vb .net application在 vb .net 应用程序中转换日期时间
【发布时间】:2014-04-22 02:39:47
【问题描述】:

日期时间格式转换有问题。在我的数据库表中,我有一列jour,格式如下:

01/02/2014 00:00:00

DD/MM/YYYY 00:00:00。当我这样做时:

        Dim date1 As String = Session("date1")
        Dim date2 As String = Session("date2")
        Dim datefirst As DateTime
        Dim dateSecond As DateTime
        Try
            datefirst = DateTime.Parse(date1)
            dateSecond = DateTime.Parse(date2)
        Catch ex As Exception

        End Try
        Dim report As ReportDocument = New ReportDocument()
        Dim reportPath As String = Server.MapPath("Reports\Prix.rpt")
        Dim ds As DataSet
        Dim cnn As SqlConnection
        Dim connectionString As String = "Data Source=HP-PC\SQLEXPRESS;Initial Catalog=ReportingFormation;Integrated Security=True;Pooling=False"
        Dim sql As String = ""
        Dim dscmd As SqlDataAdapter
        cnn = New SqlConnection(connectionString)
        cnn.Open()

        sql = "SELECT * FROM PrixProduit where ( Jour >= '" + datefirst + "'and Jour <='" + dateSecond + "')"
        dscmd = New SqlDataAdapter(sql, cnn)
        ds = New DataSet()

        dscmd.Fill(ds, "PrixProduit")

使用这些值: date1= 30/01/2014 日期2 = 06/02/2014 日期优先 = #1/30/2014# dateSecond= #2/6/2014#

所以我得到了这个错误

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

在这行代码中:

dscmd.Fill(ds, "PrixProduit")

所以我需要知道

  • 这个错误的原因是什么?
  • 我该如何解决?

【问题讨论】:

  • 数据库列的格式在这里应该无关紧要...
  • @Crono 你能解释一下你的想法吗?
  • 您说您的数据库列具有特定的日期格式。怎么会这样?如果它是 DateTime 类型(应该是),那么就没有任何类型的数据库格式,不是吗?
  • 您将日期作为字符串存储在 Session 变量中?您应该将其存储为Date(或DateTime)。这样您就不必进行StringDateTime 的转换。
  • 你应该使用parameterized查询来避免SQL injection,同时解决你的格式问题,即单引号。

标签: sql sql-server vb.net date datetime


【解决方案1】:

对于这种情况,您有两个选择,首先是更改应用程序中的date 以匹配数据库中的date。其次是在convert 期间包含样式。下面是尝试将 convert varchardate 字段的示例。首先select 将失败。但是第二个会起作用,因为我指定了 style 我期望字符串是。

DECLARE @datechar VARCHAR(12) = '16/03/2014'

SELECT CONVERT(DATE, @datechar)

SELECT CONVERT(DATE, @datechar, 103)

您需要将您的 SQL 代码修改为以下内容

sql = "SELECT * FROM PrixProduit where ( Jour >= Convert(date,'" + datefirst + "',103 ) and Jour <= Convert(date,'" + dateSecond + "',103))"

就像人们已经提到的那样,您应该避免使您的应用容易受到 sql 注入的字符串连接。因此将代码修改成这样。

sql = "SELECT * FROM PrixProduit where Jour >= Convert(date,@datefirst,103 ) and Jour <= Convert(date,@datesecond,103)"

之后,只需将参数添加到您的命令。

【讨论】:

    【解决方案2】:

    假设您使用美国日期时间格式 MM/DD/YYYY。将此行更改为:

    sql = "SELECT * FROM PrixProduit where ( Jour >= '" + datefirst.ToString("MM/dd/yyyy") + "' and Jour <='" + dateSecond.ToString("MM/dd/yyyy") + "')"
    

    ToString("MM/dd/yyyy") 将日期时间显式转换为格式 MM/dd/yyyy 以避免默认转换,这可能不是我们想要的。

    我还应该在你的解释中指出:

    “使用这些值:date1= 30/01/2014 date2 = 06/02/2014 datefirst = #1/30/2014# dateSecond= #2/6/2014# ....”

    date1 中的值是“30/01/2014”。如果这不是拼写错误,您可能会收到错误

    "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."
    

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多