这就是我所拥有的:
Dim UsDateFormat As Date = DateTime.Parse(DataDate, New System.Globalization.CultureInfo("en-US"))
Da = New OleDbDataAdapter("SELECT * FROM RawStockData WHERE PointDateTime >=#" & UsDateFormat.ToShortDateString & "# AND PointDateTime < #" & UsDateFormat.AddDays(1).ToShortDateString & "#", Con)
当选择9月份的任何日期时,它将显示30日的数据(这是唯一有数据的日期),当选择30日时,它将回复“字符串未被识别为有效日期时间”
我哪里错了?
答案 0 :(得分:1)
使用.ToShortDateString,将其替换为.ToString(“yy-MM-dd”)
至少对于ms sql,这是一种安全的日期格式,无论文化如何都可以使用,并且始终可以正确解释。
编辑: 如果您真的想要最终使用的格式,只需使用.ToString(“MM / dd / yy”)
答案 1 :(得分:0)
也许您的问题将通过DataTime.ParseExact来解决。
尝试ParseExact后,请使用SqlCommand Parameters来避免Date和DateTime的每个SQL错误。
示例代码:
Dim strYourData As String = "2013/10/02"
Dim dtYourData As DateTime = DateTime.ParseExact(strYourData, "yyyy/MM/dd", Nothing)
Dim cmdSelectRawStockData As System.Data.OleDb.OleDbCommand = yourOLEConnection.CreateCommand
cmdSelectRawStockData.CommandText = "SELECT * FROM RawStockData WHERE " _
& "PointDateTime >= @YourData1 AND " _
& "PointDateTime <= @YourData2"
cmdSelectRawStockData.Parameters.AddWithValue("@YourData1", SqlDbType.DateTime).Value = dtYourData
cmdSelectRawStockData.Parameters.AddWithValue("@YourData2", SqlDbType.DateTime).Value = DateAdd(DateInterval.Day, 1, dtYourData)
Da = New System.Data.OleDb.OleDbDataAdapter(cmdSelectRawStockData)
答案 2 :(得分:0)
使用以下代码和副代词
Dim dt As DateTime = DateTime.ParseExact(InputBookingdatesingleday.Text,
"dd/MM/yyyy",
CultureInfo.GetCultureInfo("en-GB"))
Dim dt As DateTime = DateTime.ParseExact(InputBookingdatesingleday.Text,
"dd/MM/yyyy",
CultureInfo.GetCultureInfo("en-US"))
答案 3 :(得分:0)
虽然很乱,但我用了
Da = New OleDbDataAdapter("SELECT * FROM RawStockData WHERE PointDateTime >=#" & DataDate.Date.Month.ToString & "/" & DataDate.Date.Day.ToString & "/" & DataDate.Date.Year.ToString & "# AND PointDateTime <#" & DataDate.AddDays(1).Date.Month.ToString & "/" & DataDate.AddDays(1).Date.Day.ToString & "/" & DataDate.AddDays(1).Date.Year.ToString & "#", Con)
最后。