RT/Metro商店应用如何调用SQLite数据库

发表于:2015-5-27 10:23

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:宋兴柱    来源:51Testing软件测试网采编

  使用前,要安装:SQLite for Windows Runtime (Windows 8.1)(一个VS插件)、还有Visual C++ Runtime Package(如:Microsoft Visual C++ 2013 Runtime Package for Windows),
  同时,项目生成要修改为X86或者X64,总之不能使有和AnyCPU。我这里使用的是X86.
private async void Create()
{
//数据文件保存的位置
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");
//打开创建数据库和表
using (var db = new SQLite.SQLiteConnection(dbPath))
{
//创建表
var result = db.CreateTable<Model.Person>();
await new MessageDialog("返回值:" + result).ShowAsync();
}
}
private async void Insert()
{
//连接数据库
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");
using (var db = new SQLite.SQLiteConnection(dbPath))
{
//插入操作。首先声明一个集合
ObservableCollection<Person> Collection = new ObservableCollection<Person>();
//单条插入语句
db.Insert(new Person() { FirstName = "宋兴柱1", LastName = "Sindrol" });
Collection.Add(new Person() { FirstName = "宋兴柱2", LastName = "Sindrol1" });
Collection.Add(new Person() { FirstName = "宋兴柱3", LastName = "Sindrol2" });
//多条插入集合
var result = db.InsertAll(Collection);
await new MessageDialog("返回值:" + result).ShowAsync();
}
}
private async void Update()
{
//更新语句
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");
using (var db = new SQLite.SQLiteConnection(dbPath))
{
SQLiteCommand cmd = db.CreateCommand("update person set FirstName='lisa' where LastName='Sindrol'");
var result = cmd.ExecuteNonQuery();
await new MessageDialog("返回值:" + result).ShowAsync();
}
}
private async void Delete()
{
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");
using (var db = new SQLite.SQLiteConnection(dbPath))
{
//单行删除操作
db.Delete<Person>(1);
//多行删除
var result = db.DeleteAll<Person>();
await new MessageDialog("返回值:" + result).ShowAsync();
}
}
private async void Select()
{
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");
using (var db = new SQLite.SQLiteConnection(dbPath))
{
//查询所有数据绑定到UI
List<object> list = db.Query(new TableMapping(typeof(Person)), "select *  from  Person");
gridView.ItemsSource = list;
}
}
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号