65.9K
CodeProject 正在变化。 阅读更多。
Home

在 Windows phone 8.1 中使用现有的 sqlite 数据库

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2016年3月20日

CPOL

2分钟阅读

viewsIcon

15451

downloadIcon

144

在本文中,我们将学习如何在 WP 8.1 应用程序中使用现有的 sqlite 数据库。

要求

  1. Windows OS 8.1 pro 或更高版本
  2. Windows Phone 8.1 sdk
  3. 启用 Hyper-V(用于模拟器)

目录

  1. 创建 sqlite 文件
  2. 安装 VS 2013 的 SQLite 扩展
  3. 在应用程序中设置 sqlite
  4. 执行 CRUD 操作

描述

在许多情况下,我们需要在应用程序中使用现有的数据库,因此在这里我们将看到如何在 WP 8.1 应用程序中使用现有的 sqlite 数据库。

1. 创建 sqlite 文件

为了创建 sqlite 数据库,我们将使用 Sqlite Manager,这是 Mozilla 的一个扩展。

创建一个名为 "PersonalDetails" 的表。 它包含以下列。

  1. Id - INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL
  2. Name - VARCHAR
  3. Address - VARCHAR
  4. EmailId - VARCHAR
  5. PhoneNo - VARCHAR

2. 安装 VS 2013 的 SQLite 扩展

现在,为 Windows phone 8.1 安装 Sqlite 的扩展。您可以通过使用“扩展和更新”选项直接在 Visual Studio 中下载它。或者您也可以从 这里 下载。

安装后,您将在已安装的扩展中找到它。只需在全新设置上安装一次即可。

3. 在应用程序中设置 sqlite

现在,创建一个新的空白 Windows Phone 8.1 应用程序,并从 Nuget 中安装 sqlite。您也可以通过程序包管理器控制台直接安装。

安装后,您将在项目中找到两个新的 cs 文件用于 sqlite。

现在,在应用程序中添加 sqlite 引用。

当您构建应用程序时,您将得到编译错误。

右键单击“解决方案”,转到“配置管理器”,并将平台从“Any CPU”更改为“ARM”。

4. 执行 CRUD 操作

现在创建模型类来保存对象中的此表。此模型类将是表的副本。类名必须与 Sqlite 数据库中的表名相同。

//
  public class PersonalDetails
    {
        [SQLite.AutoIncrement, SQLite.PrimaryKey]
        public int Id{get;set;}
        public string Name { get; set; }
        public string Address { get; set; }
        public string EmailId { get; set; }
        public string PhoneNo { get; set; } 
    }
//

SQLite.AutoIncrement 和 SQLite.PrimaryKey 是属性,它们显示该属性是主键并且自动递增。

将 sqlite 文件粘贴到项目中,并将它的 生成操作 设置为 内容,将 复制到输出目录 设置为 如果较新则复制

现在创建 CRUD 函数。

//
  public class DataProvider : IDisposable
    {
        private bool disposed = false;
        private string _dbName = "Employees.sqlite";
        public DataProvider()
        {
        }

        ~DataProvider()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {

                }
                disposed = true;
            }
        }

        /// <summary>
        /// Get Employee List
        /// </summary>
        /// <returns></returns>
        public List<PersonalDetails> GetEmployeeList()
        {
            List<PersonalDetails> employeeList = new List<PersonalDetails>();
            try
            {
                // Initialize the database if necessary
                using (var db = new SQLite.SQLiteConnection(_dbName))
                {
                    employeeList = db.Query<PersonalDetails>("select * from PersonalDetails").ToList();
                }//using
            }//try
            catch (Exception ex)
            {

            }//try
            return employeeList;
        }//GetEmployeeList

        public bool AddEmployee(PersonalDetails employeeInfo)
        {
            bool result = false;
            try
            {
                using (var db = new SQLite.SQLiteConnection(_dbName))
                {
                    db.RunInTransaction(() =>
                    {
                        //Insert new employee
                        db.Insert(employeeInfo);
                    });
                }//using
                result = true;
            }//try
            catch (Exception ex)
            {

            }//catch
            return result;
        }

        public bool UpdateEmployee(int id, PersonalDetails employeeInfo)
        {
            bool result = false;
            try
            {
                using (var db = new SQLite.SQLiteConnection(_dbName))
                {
                    var employee = db.Query<PersonalDetails>("select * from PersonalDetails where Id=" + id).FirstOrDefault();
                    if (employee != null)
                    {
                        //update Name and address
                        employee.Address = employeeInfo.Address;
                        employee.Name = employeeInfo.Name;
                        db.RunInTransaction(() =>
                        {
                            db.Update(employee);
                        });
                    }
                }//using
                result = true;
            }//try
            catch (Exception ex)
            {

            }//catch
            return result;
        }

        public bool DeleteEmployee(int id)
        {
            bool result = false;
            try
            {
                using (var db = new SQLite.SQLiteConnection(_dbName))
                {
                    var employee = db.Query<PersonalDetails>("select * from PersonalDetails where Id=" + id).FirstOrDefault();
                    if (employee != null)
                    {
                        //update Name and address
                        db.RunInTransaction(() =>
                        {
                            db.Delete(employee);
                        });
                    }
                }//using
                result = true;
            }//try
            catch (Exception ex)
            {

            }//catch
            return result;
        }
    }
//

在您的应用程序页面中使用这些函数。

 DataProvider provider = new DataProvider();

 //Add employee
 provider.AddEmployee(new PersonalDetails
 {
   Address = "154, Newyork",
   EmailId = "ron@ymail.com",
   Name = "Ron",
   PhoneNo = "082-445434-333"
 });

 //Get Employee
 var data = provider.GetEmployeeList();

 //Update Employee
 provider.UpdateEmployee(1,new PersonalDetails
 {
  Address = "187, Newyork",
  Name = "Ron Jan",
 });

 //Delete Employee
 provider.DeleteEmployee(1);
© . All rights reserved.