C# 在 ExecuteNonQuery 上获取受影响的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10059158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:17:45  来源:igfitidea点击:

Get affected rows on ExecuteNonQuery

c#mysql.netsqlexecutenonquery

提问by Boardy

I am currently working on a C# project and I am running an insert query which also does a select at the same time, e.g.:

我目前正在处理一个 C# 项目,并且我正在运行一个插入查询,该查询也同时执行一个选择,例如:

INSERT INTO table (SELECT * FROM table WHERE column=date)

Is there a way I can see how many rows were inserted during this query?

有没有办法可以查看在此查询期间插入了多少行?

采纳答案by John Woo

ExecuteNonQuery- returns the number of rows affected.

ExecuteNonQuery- 返回受影响的行数。

SqlCommand comm;
// other codes
int numberOfRecords = comm.ExecuteNonQuery();

回答by M.Babcock

If you run the SQL from your question in a SqlCommandand check the return value of ExecuteNonQueryit should tell you how many records were affected.

如果你从你的问题中运行 SQLSqlCommand并检查它的返回值ExecuteNonQuery应该告诉你有多少记录受到影响。

From the documentation:

文档

Return Value
Type: System.Int32
The number of rows affected.

返回值
类型:System.Int32
受影响的行数。

回答by Jfly

If you run a bulk of ExecuteNonQuery(), and commit them all in once, you can get the number of total changes after connection by read the return value from "SELECT total_changes();"

如果您运行大量的 ExecuteNonQuery(),并一次性提交它们,您可以通过读取“SELECT total_changes();”的返回值来获得连接后的总更改次数。

The function to get the total changes:

获取总变化的函数:

public static long GetTotalChanges(SQLiteConnection m_dbConnection)
        {
            string sql = "SELECT total_changes();";
            using (SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection))
            {
                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    reader.Read();
                    return (long)reader[0];
                }
            }
        }

Use it in another function:

在另一个函数中使用它:

public static long MyBulkInserts()
        {
            using (SQLiteConnection m_dbConnection = new SQLiteConnection())
            {
                m_dbConnection.Open();
                using (var cmd = new SQLiteCommand(m_dbConnection))
                {
                    using (var transaction = m_dbConnection.BeginTransaction())
                    {
                        //loop of bulk inserts
                        {
                            cmd.ExecuteNonQuery();
                        }
                        transaction.Commit();
                    }
                }
                return GetTotalChanges(m_dbConnection);
            }
        }

回答by AlejandroAlis

ExecuteNonQuery return the affected rows ONLY WHENUse Affected Rows in the connections properties is set, if not (default) returns matched rows.

ExecuteNonQuery在设置了连接属性中的 Use Affected Rows时才返回受影响的行,否则(默认)返​​回匹配的行。

回答by Ahmad Al-Ghazali

Be sure of one thing also You need to add a statement in the connection string For example:

还要确定一件事 你需要在连接字符串中添加一条语句 例如:

string const "Server=localhost; PORT=3306; Database=db; User id=root; password='';UseAffectedRows=True";
MySqlConnection con = new MySqlConnection(const);
con.Open();
MySqlCommand cmd = new MySqlCommand(con);
cmd.CommandText = "Update db set table = value where Column = value";
int numberOfRecords = cmd.ExecuteNonQuery();

Be sure of:

确保:

UseAffectedRows=True

so it will return a right value of rows affected

所以它将返回受影响行的正确值