回调请求中止查询中止

时间:2013-02-02 02:43:03

标签: c# sqlite system.data.sqlite

我正在使用一个递归函数,其中包含一个事务,但是该事务导致“回调请求的查询中止 由于ROLLBACK“错误而中止。这是我的代码:

    public Dictionary<int, string> GetFolders(string connectstring)
    {
        var connection = new SQLiteConnection(connectstring);
        connection.Open();

        Dictionary<int, string> folderdictionary = new Dictionary<int, string>();
        using (var transaction = connection.BeginTransaction())
        {
            using (SQLiteCommand command = connection.CreateCommand())
            {
                command.CommandText = "select * from moz_bookmarks AS bookmarks WHERE type=2";
                SQLiteDataReader commandread = command.ExecuteReader();
                int current_id = 0;
                List<string> currentfolders = new List<string>();

                while (commandread.Read()) // <--------------- thrown here
                {
                    var title = commandread.GetString(5);
                    current_id = commandread.GetInt32(0);

                    var parent = commandread.GetInt32(3);
                    List<string> folders = new List<string>();
                    folder = GetChild(connection, (string)commandread["title"], commandread.GetInt32(3), ref folders, true);
                    currentfolders.Add(folder);
                    folderdictionary.Add(current_id, folder);

                    folder = "";
                }

            }
            transaction.Commit();
        }
        connection.Close();
        return folderdictionary;
    }

递归本身位于GetChild函数中:

private static string GetChild(SQLiteConnection sqlite_connection2, string title, int id, ref List<string> folder, bool recursive, SQLiteTransaction transaction)
{
    if (recursive)
        folder.Add(title);
    using (SQLiteCommand folder_get = sqlite_connection2.CreateCommand())
    {
        folder_get.Connection = sqlite_connection2;
        folder_get.CommandText = "SELECT * FROM moz_bookmarks WHERE id = " + id;

        SQLiteDataReader get_folders = folder_get.ExecuteReader();
        while (get_folders.Read())
        {
            if (get_folders.GetInt32(1) == 1)
            {
                break;
            }
            else if (get_folders.GetInt32(1) == 2 && get_folders.GetInt32(0) != 1)
            {
                folder.Add(get_folders["title"].ToString());
                if (get_folders.GetInt32(3) != 0 && get_folders.GetInt32(0) != 1)
                {
                    GetChild(sqlite_connection2, (string)get_folders["title"], get_folders.GetInt32(3), ref folder, false);
                }
                break;
            }
        }
        if (recursive)
            folder.Reverse();
        get_folders.Close();
    }
    return string.Join("\\", folder);
}

如果没有SQLiteTransaction,GetChild会发生巨大的内存泄漏并最终抛出OutOfMemoryException。

0 个答案:

没有答案