将 SQLite windows 表单应用程序迁移到通用 windows 应用程序 (C#)
Migrate SQLite windows forms app to universal windows app (C#)
我制作了一个 windows 连接到本地创建的 SQLite 数据库的表单应用程序。这是一个非常简单的应用程序,主要是选择和插入数据库。
我有一个 class 检查这样的数据库是否存在,如果不存在,它会创建它。我还添加了一些用于执行查询等的方法。
现在,在 windows 表单(或控制台应用程序)中,连接非常简单:
SQLiteConnection conn = new SQLiteConnection("Data Source=sampleDB.sqlite;Version=3;");
conn.Open();
//Assume that i created table Person(int ID, string NAME)
string sql = "select * from Person";
SQLiteCommand command = new SQLiteCommand(sql, conn);
SQLiteDataReader reader = command.ExecuteReader();
while(reader.Read()){
Console.WriteLine(reader["ID"] + " | " + reader["NAME"]);
}
conn.Close();
现在,我尝试将我的应用程序从 Windows Forms 迁移到 Universal Windows App。
我做的第一件事是,我看到 System.Data.SQLite.dll 对此类应用程序无效,因此我安装了 SQLite for Universal Windows Platform,连同SQLite.Net-PCL
但现在的问题是我不知道如何像以前那样将查询作为字符串传递。
我遇到的只是我必须创建 class Person 以 Id 和 Name 作为属性,然后写这样的东西:
SQLitePlatformWinRT sqlitePlatform = new SQLitePlatformWinRT();
var db = new SQLiteConnection(sqlitePlatform, "sampleDB.sqlite");
db.CreateTable<Person>();
db.Insert(new Person(ID_PERSON, NAME_PERSON));
有什么方法可以在 Universal Windows 应用程序中使用旧方法(如 windows 表单)?
即:
//Instead of using this:
db.Insert(new Person(ID_PERSON, NAME_PERSON));
//I want to use this:
SQLiteCommand command = new SQLiteCommand("insert into Person ...", conn);
command.ExecuteNonQuery();
一种可能的方法是使用 Portable Class Library for SQLite,它支持 Sql 查询字符串,就像您在 Windows 表单中使用的那样。我们可以使用这个库代替 SQLite.Net-PCL.
要使用这个库,我们可以从 NuGet 安装它,然后像下面这样使用它:
using (var connection = new SQLitePCL.SQLiteConnection("sampleDB.sqlite"))
{
using (var statement = connection.Prepare(@"select * from Person"))
{
while (statement.Step() == SQLitePCL.SQLiteResult.ROW)
{
//TODO. For example
//Gets the value of the specified column by the column name.
var Id = (long)(statement["Id"]);
var Name = (string)statement["Name"];
//Gets the value of the specified column by the column ordinal.
//var Id = (long)(statement[0]);
//var Name = (string)statement[1];
}
}
}
更多信息,您可以参考这篇博客:The new Portable Class Library for SQLite。虽然本文适用于 Windows 8.1,但它也适用于 UWP 应用程序。
我制作了一个 windows 连接到本地创建的 SQLite 数据库的表单应用程序。这是一个非常简单的应用程序,主要是选择和插入数据库。 我有一个 class 检查这样的数据库是否存在,如果不存在,它会创建它。我还添加了一些用于执行查询等的方法。
现在,在 windows 表单(或控制台应用程序)中,连接非常简单:
SQLiteConnection conn = new SQLiteConnection("Data Source=sampleDB.sqlite;Version=3;");
conn.Open();
//Assume that i created table Person(int ID, string NAME)
string sql = "select * from Person";
SQLiteCommand command = new SQLiteCommand(sql, conn);
SQLiteDataReader reader = command.ExecuteReader();
while(reader.Read()){
Console.WriteLine(reader["ID"] + " | " + reader["NAME"]);
}
conn.Close();
现在,我尝试将我的应用程序从 Windows Forms 迁移到 Universal Windows App。 我做的第一件事是,我看到 System.Data.SQLite.dll 对此类应用程序无效,因此我安装了 SQLite for Universal Windows Platform,连同SQLite.Net-PCL
但现在的问题是我不知道如何像以前那样将查询作为字符串传递。 我遇到的只是我必须创建 class Person 以 Id 和 Name 作为属性,然后写这样的东西:
SQLitePlatformWinRT sqlitePlatform = new SQLitePlatformWinRT();
var db = new SQLiteConnection(sqlitePlatform, "sampleDB.sqlite");
db.CreateTable<Person>();
db.Insert(new Person(ID_PERSON, NAME_PERSON));
有什么方法可以在 Universal Windows 应用程序中使用旧方法(如 windows 表单)? 即:
//Instead of using this:
db.Insert(new Person(ID_PERSON, NAME_PERSON));
//I want to use this:
SQLiteCommand command = new SQLiteCommand("insert into Person ...", conn);
command.ExecuteNonQuery();
一种可能的方法是使用 Portable Class Library for SQLite,它支持 Sql 查询字符串,就像您在 Windows 表单中使用的那样。我们可以使用这个库代替 SQLite.Net-PCL.
要使用这个库,我们可以从 NuGet 安装它,然后像下面这样使用它:
using (var connection = new SQLitePCL.SQLiteConnection("sampleDB.sqlite"))
{
using (var statement = connection.Prepare(@"select * from Person"))
{
while (statement.Step() == SQLitePCL.SQLiteResult.ROW)
{
//TODO. For example
//Gets the value of the specified column by the column name.
var Id = (long)(statement["Id"]);
var Name = (string)statement["Name"];
//Gets the value of the specified column by the column ordinal.
//var Id = (long)(statement[0]);
//var Name = (string)statement[1];
}
}
}
更多信息,您可以参考这篇博客:The new Portable Class Library for SQLite。虽然本文适用于 Windows 8.1,但它也适用于 UWP 应用程序。