以编程方式添加 Adomd 参数 C#
下午都。
我正在构建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个解决方案!
还有一些小提示。
您可能会收到此错误:
“无法解析 X 参数,因为它在内部子表达式中被引用”
解决方法:在带引号的字符串中插入
@CompanyId
时,请确保该参数没有用撇号括起来(') - c# 很聪明,会转换数据类型并为您添加这些撇号。但是,您将无法在监视窗口中看到它,并且一切看起来都很好。所有项目均被返回,并且您的参数似乎被忽略。
解决方案:检查第一个
SELECT
并确保它是SELECT NON EMPTY
。A solution!
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 isSELECT NON EMPTY
.对于像这样使用 currentmember 填充参数的查询怎么样?
How about for a query like this that uses currentmember to populate the parameters?