[英]Migrate SQLite windows forms app to universal windows app (C#)
I made a windows forms app that connects to locally created SQLite db. 我创建了一个连接到本地创建的SQLite数据库的Windows窗体应用程序。 It's a pretty simple app, that mainly does selecting and inserting into database.
这是一个非常简单的应用程序,主要是选择和插入数据库。 I had a class that checked if such database exists and if not, it created it.
我有一个类检查是否存在这样的数据库,如果没有,则创建它。 I also added some methods for executing queries and such.
我还添加了一些执行查询的方法等。
Now, in windows forms (or in console app) the connection was pretty simple: 现在,在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();
Now, I tried migrating my app from Windows Forms to Universal Windows App . 现在,我尝试将我的应用程序从Windows窗体迁移到通用Windows应用程序 。 First thing I did, I saw that System.Data.SQLite.dll is not valid for such app and so I installed SQLite for Universal Windows Platform , together with SQLite.Net-PCL
我做的第一件事,我看到System.Data.SQLite.dll对这样的应用程序无效,因此我安装了SQLite for Universal Windows Platform ,以及SQLite.Net-PCL
But the problem is now that I don't know how to pass queries as string as I did before. 但现在问题是我不知道如何像以前那样将查询作为字符串传递。 All I encountered was that I had to create class Person with Id and Name as attributes, and then write something like this:
我遇到的只是我必须创建具有Id和Name作为属性的类Person,然后编写如下内容:
SQLitePlatformWinRT sqlitePlatform = new SQLitePlatformWinRT();
var db = new SQLiteConnection(sqlitePlatform, "sampleDB.sqlite");
db.CreateTable<Person>();
db.Insert(new Person(ID_PERSON, NAME_PERSON));
Is there any way, to use the old way (as in windows forms) in Universal Windows app? 有没有办法,在通用Windows应用程序中使用旧方式(如在Windows窗体中)? IE:
IE:
//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();
One possible way is using Portable Class Library for SQLite , which supports Sql Query String like what you've used in Windows Forms. 一种可能的方法是使用SQLite的可移植类库 ,它支持Sql查询字符串,就像您在Windows窗体中使用的那样。 We can use this library instead of SQLite.Net-PCL.
我们可以使用这个库而不是SQLite.Net-PCL。
To use this library, we can install it form NuGet and then use it like following: 要使用此库,我们可以从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];
}
}
}
For more information, you can refer to this blog: The new Portable Class Library for SQLite . 有关更多信息,请参阅此博客: SQLite的新可移植类库 。 Although this article is for Windows 8.1, but it also applies to UWP apps.
虽然本文适用于Windows 8.1,但它也适用于UWP应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.