Skip to main content
 首页 » 编程设计

c#之SQLite 没有创建 DateTime 字段

2025年04月02日23sharpest

在我的 C# VS2015 Windows 10 Universal App 中,我无法在数据库架构中创建 DateTime 字段。它会创建一个 bigint。我有其他表创建了一个没有问题的 DateTime 字段。

这是一个示例:(请参阅 GameDate 字段)

表 [结果]
领域:17
[季节]:nvarchar(100)
[Id]:整数
[主队名称]:nvarchar(100)
[OppTeamName]:nvarchar(100)
[游戏日期]:日期时间

Foreign Keys: 0 
Indexes: 1 
    [] PRIMARY 
        [Id] AUTOINCREMENT  
Triggers: 0 
Unique constraints: 0 
Check constraints: 0 
 
 
 
    private void AddUserButton_Click(object sender, RoutedEventArgs e) 
    { 
 
        User user = new User(); 
        user.Name = "User1"; 
        user.GameDate = Convert.ToDateTime("2015-09-02"); 
        var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "BaseBallOU.db"); 
        using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path)) 
        { 
            conn.Insert(user); 
        } 
    } 
 
    private void CreateTablesButton_Click(object sender, RoutedEventArgs e) 
    { 
        var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "BaseBallOU.db"); 
        using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path)) 
        { 
            conn.CreateTable<User>(); 
            conn.CreateTable<TeamX>(); 
        } 
    } 
} 
public class User 
{ 
 
    [SQLite.AutoIncrement, SQLite.PrimaryKey] 
    public int id { get; set; } 
    public DateTime GameDate { get; set; } 
    public string Name { get; set; } 
 
} 
 
public class TeamX 
{ 
    [SQLite.AutoIncrement, SQLite.PrimaryKey] 
    public int id { get; set; } 
    public string TeamName { get; set; } 
    public DateTime GameDate { get; set; } 
} 

以下是在表格中创建的内容:
Table [TeamX] 
Fields: 3 
    [id]: integer 
    [TeamName]: varchar 
    [GameDate]: bigint 
Foreign Keys: 0 
Indexes: 0 
Triggers: 0 
Unique constraints: 0 
Check constraints: 0 

我正在使用以下引用资料:

sqlite-net-pcl
SQLite.Net-PCL
通用应用平台的 SQLite

请您参考如下方法:

如果我没记错的话,SQLite 数据库中没有日期时间类型。
保存新对象时将日期转换为字符串,并在读取对象时将其转换回 DateTime。