SQLite是一个免费的数据库程序,适用于“轻量级”数据库应用。它支持事务和SQL调用,并且相对容易使用。将SQLite集成到UWP应用中既快速又简单;以下是将SQLite包含在C#UWP应用中的步骤。还包括一些公开可用的类,这些类简化了SQLite的使用,以及一些示例代码,展示了如何使用这些类。最后,提供了有用的下载链接和更多信息。
注意:这适用于Visual Studio 2015和2017。
应使用NuGet将SQLiteUWP库添加到解决方案中。
点击“工具”|“Nuget包管理器”|“管理解决方案的NuGet包...”,然后选择“浏览”。
搜索“SQLite.UWP.Native”。找到后,将其安装在解决方案中。
有两个名为“Sqlite.cs”和“SqliteAsync.cs”的文件,提供了一个简单的C#接口来使用SQLiteUWP库。它们可以从以下网址下载:
此网站还包含接口的详细文档。
“Sqlite.cs”文件包含一个名为“SqlConnection”的类,可用于创建和操作SQLite数据库。
“SqliteAsync.cs”包含一个名为“SqliteAsyncConnection”的类,用于异步数据库例程。根据应用是使用异步还是非异步模型,应该将这两个文件中的一个或两个添加到解决方案中。
这两个类都有连接和断开数据库的方法,创建数据库以及添加、删除和更新数据库记录的方法。此外,这些类也可以用来执行原始SQL脚本。
以下是一个Employee类的示例,它使用非异步模型:
public class Employee
{
    // 将Id属性标记为主键
    [SQLite.PrimaryKey, SQLite.AutoIncrement]
    public int Id { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public DateTime DOB { get; set; }
}
    
使用SQLiteConnection构造函数创建数据库连接,参数为数据库表的路径。
public static string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "SQLiteTest.sqlite"));
///
<summary>
///
Initialize the connection.
///
</summary>
///
<param name="path">
The optional path.
</param>
///
<returns>
True on success, false otherwise.
</returns>
public bool Initialize(string path = null)
{
    bool retVal = false;
    this.dbPath = path ?? DB_PATH;
    if (this.dbPath != null)
    {
        dbConn = new SQLiteConnection(dbPath);
        retVal = (DbConn != null);
        if (retVal)
        {
            retVal = DbConn.CreateTable<Employee>() >= 0;
        }
    }
    return retVal;
}
    
可以使用接口来选择、更新、添加和删除记录。
///
<summary>
///
Retrieve all employees.
///
</summary>
///
<returns>
The collection of employees.
</returns>
public ObservableCollection<Employee> RetrieveEmployees()
{
    List<Employee> audioList = DbConn.Table<Employee>().ToList<Employee>();
    ObservableCollection<Employee> EmployeeList = new ObservableCollection<Employee>(audioList);
    return EmployeeList;
}
///
<summary>
///
Update an existing employee.
///
</summary>
///
<param name="newRecord">
The record to update, with:
///
1.  The id of the record to update.
///
2.  The new values.
</param>
public void UpdateEmployee(Employee newRecord)
{
    var existingAudioNote = DbConn.Query<Employee>(
        "select * from Employee where Id =" + newRecord.Id).FirstOrDefault();
    if (existingAudioNote != null)
    {
        existingAudioNote.LastName = newRecord.LastName;
        existingAudioNote.FirstName = newRecord.FirstName;
        existingAudioNote.DOB = newRecord.DOB;
        DbConn.RunInTransaction(() =>
        {
            DbConn.Update(existingAudioNote);
        });
    }
}
///
<summary>
///
Insert a new employee record.  The ID field
///
is ignored.
///
</summary>
///
<param name="newEmployee">
The new employee record.
</param>
public void Insert(Employee newEmployee)
{
    DbConn.RunInTransaction(() =>
    {
        DbConn.Insert(newEmployee);
    });
}
///
<summary>
///
Delete a specific employee.
///
</summary>
///
<param name="Id">
The id of the employee to delete.
</param>
public void Delete(int Id)
{
    var existingEmployee = DbConn.Query<Employee>(
        "select * from Employee where Id =" + Id).FirstOrDefault();
    if (existingEmployee != null)
    {
        DbConn.RunInTransaction(() =>
        {
            DbConn.Delete(existingEmployee);
        });
    }
}
    
在完成使用接口后,可以“清理”资源。
///
<summary>
///
Dispose the class.
///
</summary>
public void Dispose()
{
    if (DbConn != null)
    {
        dbConn.Close();
        dbConn.Dispose();
        dbConn = null;
    }
}
    
可以从以下网址下载SQLite的可视化浏览器:
这对于测试和验证正在开发的数据库非常有用。