以编程方式添加 Adomd 参数 C#

发布于 2024-10-17 03:11:44 字数 1425 浏览 2 评论 0原文

下午都。

我正在构建一个 Web 应用程序,并尝试从 SSAS 多维数据集中提取一些数据。

我有以下 MDX,我想在 C# 中复制,但添加一些参数,即两个参数,一个用于公司 123,另一个用于位置 1:

@"SELECT NON EMPTY([Dim Unit].[All Units].[Category Group Name]) ON COLUMNS
                    FROM [Info]
                    WHERE ([Dim Company].[All Companies].&[123], 
                    [Dim Location].[All Locations].&[123]&[1])"; 

现在,我可以使用一个参数启动并运行它:

 AdomdCommand cmdPDC = conPDC.CreateCommand();
        cmdPDC.CommandText = "SELECT [Dim Unit].[All Units].[Category Group Name].Members ON 0 FROM [Info] WHERE (StrToMember(@P1)";

        string companyid = "123";

        string sP1 = "[Dim Company].&" + company;

    cmdPDC.Parameters.Add(new AdomdParameter("P1", sP1));

但是我该怎么办实现第二个参数,例如,如果我想将参数粘贴到位置?我正在按照以下思路思考,但我无法让这个小草皮工作:

AdomdCommand cmdPDC = conPDC.CreateCommand();
        cmdPDC.CommandText = "SELECT [Dim Unit].[All Units].[Category Group Name].Members ON 0 FROM [Info] WHERE (StrToMember(@P1)," + "(StrToMember(@P2))";

        string companyid = "123";
        string locationid = "1";

        string sP1 = "[Dim Company].&" + company;
        string sP2 = "[Dim Location].&" + company + "&" + location;

        cmdPDC.Parameters.Add(new AdomdParameter("P1", sP1));
        cmdPDC.Parameters.Add(new AdomdParameter("P2", sP2));

感谢收到的任何帮助或建议。

Afternoon all.

I'm building a web app and I'm attempting to pull through some data from an SSAS cube.

I have the following MDX I would like to replicate in c# but with adding a few parameters i.e. two parameteres, one for company 123 and another for location 1:

@"SELECT NON EMPTY([Dim Unit].[All Units].[Category Group Name]) ON COLUMNS
                    FROM [Info]
                    WHERE ([Dim Company].[All Companies].&[123], 
                    [Dim Location].[All Locations].&[123]&[1])"; 

Now, I can get this up and running with one parameter:

 AdomdCommand cmdPDC = conPDC.CreateCommand();
        cmdPDC.CommandText = "SELECT [Dim Unit].[All Units].[Category Group Name].Members ON 0 FROM [Info] WHERE (StrToMember(@P1)";

        string companyid = "123";

        string sP1 = "[Dim Company].&" + company;

    cmdPDC.Parameters.Add(new AdomdParameter("P1", sP1));

But how do I then implement a second parameter, for example, if I wanted to stick a parameter in for location? I was thinking along the lines ofbelow but I can't get the little sod to work:

AdomdCommand cmdPDC = conPDC.CreateCommand();
        cmdPDC.CommandText = "SELECT [Dim Unit].[All Units].[Category Group Name].Members ON 0 FROM [Info] WHERE (StrToMember(@P1)," + "(StrToMember(@P2))";

        string companyid = "123";
        string locationid = "1";

        string sP1 = "[Dim Company].&" + company;
        string sP2 = "[Dim Location].&" + company + "&" + location;

        cmdPDC.Parameters.Add(new AdomdParameter("P1", sP1));
        cmdPDC.Parameters.Add(new AdomdParameter("P2", sP2));

Any help or advice gratefully received.

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

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

发布评论

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

评论(2

梦魇绽荼蘼 2024-10-24 03:11:45

一个解决方案!

AdomdCommand cmdPDC = conPDC.CreateCommand();
cmdPDC.CommandText = "SELECT NON EMPTY [Dim Unit].[All Units].[Category Name].Members ON 0 FROM [MY CUBE] WHERE (StrToMember(@CompanyId),StrToMember(@LocationId))";

string companyid = "[123]";
string locationid = "[1]";

string sCompanyId = "[Dim Company].&" + companyid;
string sLocationId = "[Dim Location].&" + companyid + "&" + locationid;

cmdPDC.Parameters.Add(new AdomdParameter("CompanyId", sCompanyId));
cmdPDC.Parameters.Add(new AdomdParameter("LocationId", sLocationId));

还有一些小提示。

您可能会收到此错误:

“无法解析 X 参数,因为它在内部子表达式中被引用”

解决方法:在带引号的字符串中插入 @CompanyId 时,请确保该参数没有用撇号括起来(') - c# 很聪明,会转换数据类型并为您添加这些撇号。但是,您将无法在监视窗口中看到它,并且一切看起来都很好。

所有项目均被返回,并且您的参数似乎被忽略。

解决方案:检查第一个 SELECT 并确保它是 SELECT NON EMPTY

A solution!

AdomdCommand cmdPDC = conPDC.CreateCommand();
cmdPDC.CommandText = "SELECT NON EMPTY [Dim Unit].[All Units].[Category Name].Members ON 0 FROM [MY CUBE] WHERE (StrToMember(@CompanyId),StrToMember(@LocationId))";

string companyid = "[123]";
string locationid = "[1]";

string sCompanyId = "[Dim Company].&" + companyid;
string sLocationId = "[Dim Location].&" + companyid + "&" + locationid;

cmdPDC.Parameters.Add(new AdomdParameter("CompanyId", sCompanyId));
cmdPDC.Parameters.Add(new AdomdParameter("LocationId", sLocationId));

And a few little pointers.

You may receive this error:

"X parameter could not be resolved because it was referenced in an inner subexpression"

Resolution: When inserting the @CompanyId in the quoted string, make sure the parameter is not enclosed in apostrophes (') - c# is clever and will convert the data type and add these apostrophes in for you. However, you won't be able to see it in a watch window and all would appear to be fine.

All items are returned and it would appear that your parameters are being ignored.

Resolution: Check the very first SELECT and make sure it is SELECT NON EMPTY.

三月梨花 2024-10-24 03:11:45
SELECT NON EMPTY { 
[Measures].[Phone Count], 
[Measures].[Mail Count], 
[Measures].[Individual Count] 
} ON COLUMNS 
FROM [PyramidReports-US] 
WHERE ( 
 (STRTOSET(@PoliticalGeographyByCD), 
 [Political Geography].[By CD].[State].currentmember ), 
 (STRTOSET(@GenderGender), 
 [Gender].[Gender].currentmember ), 
 (STRTOSET(@ModelAffinityAffinityQuartile), 
 [Model Affinity].[Affinity Quartile].currentmember 
  ) 

对于像这样使用 currentmember 填充参数的查询怎么样?

SELECT NON EMPTY { 
[Measures].[Phone Count], 
[Measures].[Mail Count], 
[Measures].[Individual Count] 
} ON COLUMNS 
FROM [PyramidReports-US] 
WHERE ( 
 (STRTOSET(@PoliticalGeographyByCD), 
 [Political Geography].[By CD].[State].currentmember ), 
 (STRTOSET(@GenderGender), 
 [Gender].[Gender].currentmember ), 
 (STRTOSET(@ModelAffinityAffinityQuartile), 
 [Model Affinity].[Affinity Quartile].currentmember 
  ) 

How about for a query like this that uses currentmember to populate the parameters?

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