【发布时间】:2016-10-03 12:49:47
【问题描述】:
我制作了一个连接到本地创建的 SQLite 数据库的 Windows 窗体应用程序。这是一个非常简单的应用程序,主要用于选择和插入数据库。 我有一个类检查这样的数据库是否存在,如果不存在,它会创建它。我还添加了一些用于执行查询等的方法。
现在,在 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 窗体 迁移到 Universal Windows 应用程序。 我做的第一件事是,我发现 System.Data.SQLite.dll 对这样的应用程序无效,所以我安装了 SQLite for Universal Windows Platform 和 SQLite.Net-PCL
但现在的问题是我不知道如何像以前那样将查询作为字符串传递。 我遇到的只是我必须创建具有 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));
有什么方法可以在通用 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();
【问题讨论】:
标签: c# winforms sqlite windows-10-universal