我正在尝试将一些 csv 样式 file.txt 内容插入本地 SQLite
数据库,我处理文件读取、内容处理并获取一些字符串:
INSERT OR REPLACE INTO mytab (id,name,adress) VALUES(1,john,adress1)
INSERT OR REPLACE INTO mytab (id,name,adress) VALUES(2,marry,adress2)
INSERT OR REPLACE INTO mytab (id,name,adress) VALUES(3,lama,ruadress3)
//...
现在我想打开一个 sqlite 事务,执行所有这些插入操作,然后关闭事务。我正在使用 SQLite.Net-PCL
和 SQLite for Universal App Platform
,我该怎么做?
我尝试打开这样的连接:
var sqlpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Mydb.sqlite");
SQLiteConnection conn = new SQLiteConnection(new SQLitePlatformWinRT(), sqlpath);
//I'am trying to open transaction - but get ERROR -> SQLiteCommand does not contain contstructor that takes 2 arguments
SQLiteCommand cmd = new SQLiteCommand("BEGIN", conn);
最佳答案
SQLite.Net-PCL
中的 SQLiteConnection
类具有 BeginTransaction
、Commit
和 Rollback
方法,因此您可以在 BeginTransaction 和 Commit 之间包装命令:
// Open connection
SQLiteConnection conn = new SQLiteConnection(new SQLitePlatformWinRT(), sqlpath);
try {
// Start transaction
conn.BeginTransaction();
try {
// Execute commands...
// Commit transaction
conn.Commit();
}
catch (Exception) {
// Rollback transaction
conn.Rollback();
}
}
finally {
// Close connection
conn.Close();
}
关于c# - UWP 将 sqlite 插入查询插入 SQL 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434788/