【问题标题】:Executing multi query using MySQL .Net Core ADO使用 MySQL .Net Core ADO 执行多查询
【发布时间】:2017-05-04 13:53:22
【问题描述】:

我正在尝试为一个新的 asp.net 核心站点创建一个 MySQL 数据库库。在 MVC5 时代,我使用了类似的模式,使用 Datatable 和 DataSet。那些可怕的数据结构已从 Core 中删除,我想知道执行具有多个结果集的查询的最佳方法是什么。

例如,

  public static IEnumerable<IEnumerable<IDataRecord>> ExecuteMultiQuery(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] commandParameters) {

        var returnObject = new List<List<IDataRecord>>();

        using (MySqlCommand command = PrepareCommand(connection, commandType, commandText, commandParameters)) {
            try {
                connection.Open();

                command.Prepare();

                MySqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

                do {

                    List<IDataRecord> resultSet = new List<IDataRecord>();

                    while (reader.Read()) {
                        resultSet.Add(reader);
                    }

                    returnObject.Add(resultSet);

                } while (reader.NextResult());

            }
            finally {
                connection.Close();
            }

            command.Parameters.Clear();

        }

        return returnObject;
    }

但是,在单步执行代码时,我注意到集合实际上从未保存任何数据。我已经测试了我的存储过程,这正确地返回了所需的行。另外,我有一个类似于 Dapper 的定制微 ORM,它负责 POCO 绑定。我对此感到满意,并且不希望依赖外部 nuget 包。

我只是无法理解为什么该方法没有填充集合...

任何指导将不胜感激。

P.S PrepareCommand 方法调用是私有的,只返回一个正确填充的带有参数的 MySqlCommand。

P.P.S 不要向我提及 EF,因为这是一个糟糕的框架,具有繁琐的绑定时间。我可以在大约 75-90 毫秒内以类似的 ExecuteReader 方法绑定模型(不是 Dapper 快但足够快),所以我知道我创建的 DBUtiltiy 在其他场景中工作 - 只是不适用于多查询。

谢谢大家

【问题讨论】:

    标签: c# mysql orm ado.net asp.net-core


    【解决方案1】:

    好吧,已经有一段时间了,这里只有翻滚杂草。但我只能建议等待新版本的 Core,它将包括旧的 DataSet、DataTable 库以及 Transactions!。

    作为一种解决方法,我返回一个 IEnumerable 并使用反射来填充 POCO。很脏,但它会作为一种妥协,直到 Core 是一个完全形成的框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 2018-06-01
      • 2015-03-23
      相关资源
      最近更新 更多