[C#]UWP 查询将其打包 sqlite 插入到 sql 事务

发布时间: 2017/2/24 3:41:03
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图插入到本地一些 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-PCLSQLite 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);

解决方法 1:

SQLiteConnection类的 SQLite.Net-PCLBeginTransactionCommitRollback 方法,以便你可以把包起来 BeginTransaction 与提交之间的命令︰

// 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();
}
赞助商