sqlite - 此附加代码的任何改进(在 sqlite 中以事务方式运行多个 sql 命令)

发布于 2024-08-25 04:46:19 字数 1984 浏览 5 评论 0原文

这段代码可靠吗?我尝试使用“using”等。基本上是一种作为 SQL 命令序列列表传递以针对 Sqlite 数据库运行的方法。

我认为在 sqlite 中默认情况下在单个连接中运行的所有命令都是以事务方式处理的,这是真的吗?这是真的吗?即我不必(并且目前还没有进入代码)BeginTransaction 或CommitTransaction。

它使用 http://sqlite.phxsoftware.com/ 作为 sqlite ADO.net 数据库提供程序。

第一次尝试

private int ExecuteNonQueryTransactionally(List<string> sqlList)
{
    int totalRowsUpdated = 0;

    using (var conn = new SQLiteConnection(_connectionString))
    {
        // Open connection (one connection so should be transactional - confirm)
        conn.Open();

        // Apply each SQL statement passed in to sqlList
        foreach (string s in sqlList)
        {
            using (var cmd = new SQLiteCommand(conn))
            {
                cmd.CommandText = s;
                totalRowsUpdated = totalRowsUpdated + cmd.ExecuteNonQuery();
            }
        }
    }

    return totalRowsUpdated;
}

第三次尝试

这是怎么样?

private int ExecuteNonQueryTransactionally(List<string> sqlList)
{
    int totalRowsUpdated = 0;

    using (var conn = new SQLiteConnection(_connectionString))
    {
        conn.Open();
        using (var trans = conn.BeginTransaction())
        {

            try
            {
                // Apply each SQL statement passed in to sqlList
                foreach (string s in sqlList)
                {
                    using (var cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = s;
                        totalRowsUpdated = totalRowsUpdated + cmd.ExecuteNonQuery();
                    }
                }

                trans.Commit();
            }
            catch (SQLiteException ex)
            {
                trans.Rollback();
                throw;
            }


        }
    }
    return totalRowsUpdated;
}

谢谢

Is this code solid? I've tried to use "using" etc. Basically a method to pass as sequenced list of SQL commands to be run against a Sqlite database.

I assume it is true that in sqlite by default all commands run in a single connection are handled transactionally? Is this true? i.e. I should not have to (and haven't got in the code at the moment) a BeginTransaction, or CommitTransaction.

It's using http://sqlite.phxsoftware.com/ as the sqlite ADO.net database provider.

1st TRY

private int ExecuteNonQueryTransactionally(List<string> sqlList)
{
    int totalRowsUpdated = 0;

    using (var conn = new SQLiteConnection(_connectionString))
    {
        // Open connection (one connection so should be transactional - confirm)
        conn.Open();

        // Apply each SQL statement passed in to sqlList
        foreach (string s in sqlList)
        {
            using (var cmd = new SQLiteCommand(conn))
            {
                cmd.CommandText = s;
                totalRowsUpdated = totalRowsUpdated + cmd.ExecuteNonQuery();
            }
        }
    }

    return totalRowsUpdated;
}

3rd TRY

How is this?

private int ExecuteNonQueryTransactionally(List<string> sqlList)
{
    int totalRowsUpdated = 0;

    using (var conn = new SQLiteConnection(_connectionString))
    {
        conn.Open();
        using (var trans = conn.BeginTransaction())
        {

            try
            {
                // Apply each SQL statement passed in to sqlList
                foreach (string s in sqlList)
                {
                    using (var cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = s;
                        totalRowsUpdated = totalRowsUpdated + cmd.ExecuteNonQuery();
                    }
                }

                trans.Commit();
            }
            catch (SQLiteException ex)
            {
                trans.Rollback();
                throw;
            }


        }
    }
    return totalRowsUpdated;
}

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-09-01 04:46:41

是的,确实如此,每个 SQLite 非嵌套命令都嵌套在一个事务中。因此,如果您需要运行多个查询,而不获取结果,那么显式启动事务、执行查询并提交会带来很多好处。

Yes, it's true, each SQLite unnested command is nested in a transaction. So that if you need to run several queries, without fetching the result, there is much gain is explicitly starting a transaction, doing your queries, and committing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文