zoukankan      html  css  js  c++  java
  • RT/Metro商店应用如何调用SQLite数据库

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

    使用前,要安装: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;
                }
            }

    Person类如下图所示:

      class Person
        {
    
            [SQLite.AutoIncrement, SQLite.PrimaryKey]
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

    数据库效果图:

  • 相关阅读:
    C#通过模板导出Word(文字,表格,图片) 转载
    转载:mysql新手入门安装配置:mysql 8.0.13 zip安装,初始配置,修改密码(经测试管用)
    转载:MySQL 8.0.19安装教程(windows 64位)
    VS2010上winform打包发布、打包安装程序
    asp.net core mvc权限控制:分配权限
    asp.net core mvc权限控制:权限控制介绍
    实测可用-免费屏幕录制软件下载地址
    Win10激活工具-Win7激活工具-Office激活工具-KMS激活工具汉化版x64下载-实测可用
    C#-WPF实现抽屉式风格主题框架源码-使用MaterialDesignThemes实现WPF炫酷漂亮的效果-提供Demo下载
    C#实现图片暗通道去雾算法-Demo-提供代码实例下载地址
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/4529885.html
Copyright © 2011-2022 走看看