public Task RunInTransactionAsync(Action <SQLiteConnection> action) { if (action == null) { throw new ArgumentNullException("action"); } return(Task.Factory.StartNew(() => { SQLiteConnectionWithLock conn = GetConnection(); using (conn.Lock()) { conn.BeginTransaction(); try { action(conn); conn.Commit(); } catch (Exception) { conn.Rollback(); throw; } } }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default)); }
public Task RunInTransactionAsync(Action <SQLiteConnection> action) { if (action == null) { throw new ArgumentNullException("action"); } return(_taskFactory.StartNew(() => { SQLiteConnectionWithLock conn = GetConnection(); using (conn.Lock()) { conn.BeginTransaction(); try { action(conn); conn.Commit(); } catch (Exception) { conn.Rollback(); throw; } } })); }
/// <summary> /// 批量导入数据 /// </summary> /// <param name="data">JSON格式数据,每个key对应一个type,包含一个array</param> public void ImportData(JObject data) { _db.BeginTransaction(); foreach (var it in data) { var count = 0; foreach (var obj in it.Value) { ReceiveDataObject(obj.Value <JObject>(), it.Key); ++count; } Log?.Information("Import {n} {type} records.", count, it.Key); } _db.Commit(); }
private void AddRecords() { SqLiteUtilitiesAlt utilities; int maxValue = 999; utilities = new SqLiteUtilitiesAlt(this); TimeSpan original, alternative; try { utilities.OpenConnection(); var stopWatch = Stopwatch.StartNew(); for (int i = 0; i <= maxValue; i++) { utilities.AddRecord("test", "person", i, "12345678901234567890123456789012345678901234567890"); } stopWatch.Stop(); original = stopWatch.Elapsed; utilities.CloseConnection(); var newConnection = new SQLiteConnectionWithLock(new SQLitePlatformAndroid(), new SQLiteConnectionString(Path.Combine(Environment.ExternalStorageDirectory.AbsolutePath, "async.db"), true)); newConnection.DropTable <Record>(); newConnection.CreateTable <Record>(); stopWatch.Restart(); newConnection.BeginTransaction(); for (int i = 0; i <= maxValue; i++) { newConnection.Insert(new Record() { FirstName = "test", LastName = "person", Index = i, Misc = "12345678901234567890123456789012345678901234567890" }); } newConnection.Commit(); stopWatch.Stop(); alternative = stopWatch.Elapsed; } catch (Exception ex) { AlertDialog.Builder dlgException = new AlertDialog.Builder(this); dlgException.SetMessage("An error has occurred adding records: " + ex.Message); dlgException.SetTitle("Error"); dlgException.SetPositiveButton("OK", (sender, args) => { }); dlgException.SetCancelable(true); dlgException.Create().Show(); return; } AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); dlgAlert.SetMessage(string.Format("All records written to database. Original time: {0}, alternative time: {1}", original, alternative)); dlgAlert.SetTitle("Success"); dlgAlert.SetPositiveButton("OK", (sender, args) => { }); dlgAlert.SetCancelable(true); dlgAlert.Create().Show(); return; }