2013-09-23 50 views
1

我試圖使用日期和時間選擇器控件的WinRT 8.1所示:保存日期和時間與WinRT中8.1使用SQLite datetime數據類型:System.InvalidCastException

日期選擇控件

enter image description here

時間選擇器控件:

enter image description here

現在宣佈他們在我的表爲s hown:

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 

現在的問題是,同時節省我得到一個異常,如下所示:

enter image description here

所以任何人都會讓我知道如何使用正確的數據類型將日期保存到我的表中。

回答

1

DateTimeFormatter只能在格式化要顯示給用戶的字符串時使用。由於您嘗試爲後端(數據庫)格式化字符串,因此應使用標準格式。另外,SQLite需要日期的特定格式作爲標準的ISO8601字符串(「YYYY-MM-DD HH:MM:SS.SSS」)。你應該使用.Net日期格式:

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

@ Eric-謝謝你的回覆......這讓我變得簡單:) – coder

相關問題