vb.net - 在WinRT 8.1中使用sqlite使用datetime数据类型保存日期和时间:System.InvalidCastException

标签 vb.net datetime sqlite windows-runtime type-conversion

我试图在WinRT 8.1中使用日期和时间选择器控件,如下所示:

日期选择器控件



时间选择器控件:



现在在我的表中声明它们,如下所示:

Public Class mydata
        <MaxLength(5), PrimaryKey> _
        Public Property name() As String
        <MaxLength(50)> _
        Public Property date1() As DateTime
        <MaxLength(50)> _
        Public Property date2() As DateTime
End Class 


现在,我尝试如图所示使用它们:

Dim dateFormatter As New DateTimeFormatter("shortdate")
Dim timeFormatter As New DateTimeFormatter("shorttime")

Dim selectedDate1 As DateTimeOffset = Me.selectedDate1.Date
Dim combinedate1Value As New DateTimeOffset(New Date(selectedDate1.Year, selectedDate1.Month, selectedDate1.Day) + Me.selectedDate1.Time)


Dim selectedDate2 As DateTimeOffset = Me.selectedDate2.Date
Dim combineddate2Value As New DateTimeOffset(New Date(selectedDate2.Year, selectedDate2.Month, selectedDate2.Day) + Me.selectedDate2.Time)


If CustomerName.Text <> "" Then
      Dim dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "mydata.db")
      Using db = New SQLite.SQLiteConnection(dbpath)
         ' Create the tables if they don't exist
          db.Insert(New person() With {.name = CustomerName.Text.ToString(), .date1 = dateFormatter.Format(combinedate1Value) & " " & timeFormatter.Format(combinedate1Value), .date2= dateFormatter.Format(combinedate2Value) & " " & timeFormatter.Format(combinedate2Value)})

          db.Commit()
          db.Dispose()
          db.Close()
          Dim line = New MessageDialog("Records Inserted")
          Await line.ShowAsync()
      End Using
Else
      Throw New NullReferenceException("Please enter the required fields")
End If


现在的问题是在保存时出现如下所示的异常:



所以有人让我知道如何使用正确的数据类型将日期保存到表中。

最佳答案

仅当格式化要显示给用户的字符串时,才应使用DateTimeFormatter。由于您尝试格式化后端(数据库)的字符串,因此应使用标准格式。另外,SQLite需要日期的特定格式作为标准ISO8601字符串(“ YYYY-MM-DD HH:MM:SS.SSS”)。您应该使用.Net日期格式:

combinedate1Value.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)); 

关于vb.net - 在WinRT 8.1中使用sqlite使用datetime数据类型保存日期和时间:System.InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18965619/

相关文章:

mysql - 需要在 MySQL 中选择日期时间范围

在 C 中捕获 Sqlite 命令行函数错误

javascript - asp.net GetElementByid().value 元素/属性不可用

.net - Decimal.ToDouble Vs CType(x, Double) 什么是推荐/首选

vb.net - 已经运行的应用程序现在出现套接字错误 10013

sql-server - SQL Server : convert datetime in place in table

python - 将日期时间打印为 DD-MM-YYYY

c# - MSDN 的 Visual Studio 快捷方式

mysql - sql计算多行类里面的当前学生

从多个表中删除的 SQLite 查询