7

This one is related to spatilite also (not only SQLite)

I have a file database (xyz.db) which I am using by SQLiteconnection (SQLiteconnection is extends to spatialite).

I have so many records needs to update into database.

                for (int y = 0; y < castarraylist.Count; y++)
                {
                    string s = Convert.ToString(castarraylist[y]);

                    string[] h = s.Split(':');

                    SQLiteCommand sqlqctSQL4 = new SQLiteCommand("UPDATE temp2 SET GEOM = " + h[0] + "WHERE " + dtsqlquery2.Columns[0] + "=" + h[1] + "", con);
                    sqlqctSQL4.ExecuteNonQuery();

                    x = x + 1;
                }

At above logic castarraylist is Arraylist which contains value which need to process into database.

When I checked above code updating around 400 records in 1 minute.

Is there any way by which I can able to improve performance ?

NOTE :: (File database is not thread-safe)

2. BEGIN TRANSACTION

Let's suppose I like to run two (or millions) update statement with single transaction in Spatialite.. is it possible ?

I read online and prepare below statement for me (but not get success)

BEGIN TRANSACTION;
UPDATE builtuparea_luxbel SET ADMIN_LEVEL = 6 where PK_UID = 2;
UPDATE builtuparea_luxbel SET ADMIN_LEVEL = 6 where PK_UID = 3;
COMMIT TRANSACTION; 

Above statement not updating records in my database. is SQLite not support BEGIN TRANSACTION ? is there anything which I missing ?

And If I need to run individual statement then it's taking too much time to update as said above...

4

2 に答える 2

5
  • データベース トランザクションの主な目的は、すべてを完了させるか、内部で何かが失敗した場合は何もしないことです。

  • CommandText プロパティを変更して同じSQLiteCommand オブジェクトを再利用し、何度も実行する方が高速かもしれませんが、メモリ オーバーヘッドが発生します。実行するクエリが大量にある場合は、使用後にオブジェクトを破棄し、新しいもの;

ADO.NET トランザクションの一般的なパターンは次のとおりです。

using (var tra = cn.BeginTransaction())
{
    try
    { 
        foreach(var myQuery in myQueries)
        { 
            using (var cd = new SQLiteCommand(myQuery, cn, tra))
            {
                cd.ExecuteNonQuery();
            }
        }

        tra.Commit();
    }
    catch(Exception ex)
    {
        tra.Rollback();
        Console.Error.Writeline("I did nothing, because something wrong happened: {0}", ex);
        throw;
    }
}
于 2014-07-09T16:02:48.303 に答える