1. 下载sqlite.cs:
https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs
2. 创建 测试实体:
[Table("Items")] public class Stock { [PrimaryKey, AutoIncrement, Column("_id")] public int Id { get; set; } [MaxLength(8)] public string Symbol { get; set; } }
3. 示例
3.1 定义路径
3.2 创建连接
3.3 创建表
3.4 插入实体
3.5 打印结果
示例代码如下:
string dbPath = string.Format("{0}//{1}", Environment.GetFolderPath(Environment.SpecialFolder.Personal), "ormdemo.db3"); var db = new SQLiteConnection(dbPath); db.CreateTable<Stock>(); if (!db.Table<Stock>().Any()) { // only insert the data if it doesn‘t already exist var newStock = new Stock { Symbol = "AAPL" }; db.Insert(newStock); newStock = new Stock { Symbol = "GOOG" }; db.Insert(newStock); newStock = new Stock { Symbol = "MSFT" }; db.Insert(newStock); } //Console.WriteLine("Reading data"); var sb = new StringBuffer(); var table = db.Table<Stock>(); foreach (var s in table) { sb.Append(s.Id + " " + s.Symbol); } return sb.ToString();
时间: 2024-10-27 03:23:29