c# MySqlConnector 查询和 FOUND_ROWS()

c# MySqlConnector query and FOUND_ROWS()

前言:我对 MySqlConnector for c# 和一般 MySql 还很陌生。

我想实现的目标是执行一个查询,然后通过FOUND_ROWS获取SqlDataReader中返回的记录数。

这是我的代码 运行:

    public MySqlDataReader Execute()
    {
        RecordCount = 0;

        mCommand = new MySqlCommand("SELECT a,b,c FROM TABLE my_table where field_x=hello", connection);

        MySqlCommand result_count_cmd = new MySqlCommand("Select FOUND_ROWS()", mCommand.Connection);



        try
        {
            // create mysql command and execute it
            mReader = mCommand.ExecuteReader();

            // get the number of rows returned by the query
            RecordCount = Convert.ToInt32(result_count_cmd.ExecuteScalar());

        }
        catch (Exception ex)
        {
            Console.WriteLine("Error Running Query: " + mCommand.CommandText);
            Console.WriteLine(ex.ToString());
        }

        return mReader;

    }

我面临的问题是,在尝试执行 FOUND_ROWS() 查询时,它无法告诉我无法在另一个未关闭的同一连接上创建新的 DataReader reader。这对我来说听起来很公平,有没有什么方法可以检索查询返回的行数,运行 与 SELECT COUNT [...] 完全相同的查询作为不同的数据库操作?或者有没有办法在 DataReader 仍然打开时调用 FOUND_ROWS 查询?

编辑:

 try
    {
      // create mysql command and execute it
       mReader = mCommand.ExecuteReader();
       int RecordCount=0;
       while (mReader.Read())
           { 
             ///do what you want with the result
             RecordCount++; //count number of rows returned
           }
     }