【问题标题】:Limiting the number of retrieved rows using Fill in ADOMD使用 Fill in ADOMD 限制检索的行数
【发布时间】:2020-10-01 07:24:40
【问题描述】:

以下 C# 代码运行 DAX 语句并检索 DataTable。这很好用,但现在我需要从数据库中检索最多 N 行。有没有办法限制Fill 函数返回的行数?如果没有,我怎样才能检索前 N 行?请注意,我需要为任何 DAX 语句保留此通用性,因此您不应更改 DAX 本身。另外,我不想检索所有数据然后取前 N 行,因为数据可能太大。

    public static DataTable runDaxStatement(int maxRows) {

        var con = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
        AdomdConnection conn = new AdomdConnection(con);

        DataSet ds = new DataSet();
        ds.EnforceConstraints = false;
        AdomdCommand cmd = new AdomdCommand("evaluate customers", conn);
        AdomdDataAdapter da = new AdomdDataAdapter(cmd);
        da.Fill(ds);

        return ds.Tables[0];

    }

【问题讨论】:

  • 遍历表以获得所需的计数,将数据传递到新表。
  • 我想过,但这意味着我必须从数据库中检索所有行,我想限制这个数字,因为数据可能非常大。
  • da.Fill(ds,<startRecord>,<maxRecords>,<srcTableName>) - 你有这种重载的方法吗? (在 ADO.NET 中 - 可用)
  • @user2932057 这有效:da.Fill(0, maxRows, dt);其中 dt 是要返回的 DataTable

标签: c# dax adomd.net


【解决方案1】:

在文档中遇到以下TOPN function

这可用于返回指定表的前N行。

例如

public static DataTable runDaxStatement(int maxRows) {
    var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
    using(AdomdConnection connection = new AdomdConnection(connectionString)) {
        string commandText = $"EVALUATE TOPN({maxRows}, customers, <orderBy_expression_here>)";
        AdomdCommand command = connection.CreateCommand();
        command.CommandText = commandText;

        DataSet dataSet = new DataSet(){
            EnforceConstraints = false
        }

        AdomdDataAdapter adapter = new AdomdDataAdapter(command);
        adapter.Fill(dataSet);

        return dataSet.Tables[0];
    }
}

【讨论】:

  • 谢谢,但这意味着我需要更改 DAX 语句,要求之一是不要更改它。
  • @ps0604 从技术上讲,您需要更改语句才能使用限制行检索的 dax 函数。检查链接的文档
  • @ps0604 查看这两个语句,您将看到它是如何针对与以前相同的 tabla 的。
  • 在 Fill 语句中使用参数怎么样?正如 user2932057 所说,我可以指定一个限制。我无法更改语句,因为它是由用户输入的。
  • @ps0604 在底层它基本上完全按照我最初的建议做。它将拉出所有行然后应用过滤器
猜你喜欢
  • 1970-01-01
  • 2017-05-25
  • 2011-12-19
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 2014-01-06
相关资源
最近更新 更多