1

I am trying to insert some csv style file.txt content to a local SQLite dataBase, I handle file reading, content process and get some strings:

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)
//...

Now I want to open an sqlite transaction, do all this inserts and then to close transaction. I am using SQLite.Net-PCL and SQLite for Universal App Platform, how can I do that?

I tryed to open a connection like this:

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);
Vasile Doe
  • 1,674
  • 1
  • 24
  • 40

1 Answers1

2

The SQLiteConnection class in SQLite.Net-PCL have BeginTransaction, Commit and Rollback methods, so you can wrap commands between BeginTransaction and 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();
}
Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28