0

I need the ability to backup a sqlite database without locking it for inserts. I can do this with Sql Server just fine but i was hoping there was a way to do this with Sqlite.

The backup code is:

            using (var location = new SQLiteConnection(sqliteConnectionString))
            using (var destination = new SQLiteConnection(buConnectionString))
            {
                location.Open();
                destination.Open();
                location.BackupDatabase(destination, "main", "main", -1, null, 0);
            }

and the code that is throwing there error is:

            using (var connection = new SQLiteConnection(constring))
            {
                connection.Open();
                using (var cmd = new SQLiteCommand(sql, connection))
                {
                    using (var tx = connection.BeginTransaction())
                    {
                        foreach (var t in data)
                        {
                            cmd.Parameters.AddWithValue("$Id", t.TagId);
                            cmd.Parameters.AddWithValue("$StampedIn", t.StampedIn.
                                ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture));
                            cmd.Parameters.AddWithValue("$StampedOut", t.StampedOut.
                                ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture));

                        }
                        cmd.ExecuteNonQuery();
                        tx.Commit();
                    }
                }
                connection.Close();
            }

The data object has roughly 3000 items in it and each is logged/updated once a second. Any ideas on how to do a backup with out locking the database would be helpful. Thanks everyone

BDevAcc
  • 1
  • 1
  • read the non locking backup information from [their website](https://www.sqlite.org/backup.html) – Franck Jan 23 '20 at 18:04
  • And after you read that, you probably want [this post](https://stackoverflow.com/questions/11425202/is-it-possible-to-call-a-c-function-from-c-net) – Jacob H Jan 23 '20 at 18:05
  • Not related to your question, but are you sure that the code that insert items is working? I see a lot of parameters add but not a single parameters clear between loops. In other words, if _data_ contains 3000 items you are adding 9000 parameters to your command. – Steve Jan 23 '20 at 18:05
  • @Steve That was a formatting mistake while i was trying to format the code in the question. – BDevAcc Jan 23 '20 at 22:04

0 Answers0