C# 使用ADOMD XMLReader从SSAS多维数据集获取数据
我有一个多维数据集,我正在尝试使用以下代码检索数据。我不知道查询将返回多少列和行我只想读取每行上每列的值。C# 使用ADOMD XMLReader从SSAS多维数据集获取数据,c#,asp.net,cube,ssas-2008,adomd.net,C#,Asp.net,Cube,Ssas 2008,Adomd.net,我有一个多维数据集,我正在尝试使用以下代码检索数据。我不知道查询将返回多少列和行我只想读取每行上每列的值。 void OutputDataWithXML() { //Open a connection to the local server. AdomdConnection conn = new AdomdConnection("Data Source=localhost"); conn.Open();
void OutputDataWithXML()
{
//Open a connection to the local server.
AdomdConnection conn = new AdomdConnection("Data Source=localhost");
conn.Open();
//Create a command to retrieve the data.
AdomdCommand cmd = new AdomdCommand(@"WITH MEMBER [Measures].[FreightCostPerOrder] AS
[Measures].[Reseller Freight Cost]/[Measures].[Reseller Order Quantity],
FORMAT_STRING = 'Currency'
SELECT [Geography].[Geography].[Country].&[United States].Children ON ROWS,
[Date].[Calendar].[Calendar Year] ON COLUMNS
FROM [Adventure Works]
WHERE [Measures].[FreightCostPerOrder]", conn);
//Execute the command, retrieving an XmlReader.
System.Xml.XmlReader reader = cmd.ExecuteXmlReader();
**// How to get the values form each column here ????
// I just want to read value of each column going over each row**
Console.WriteLine(reader.ReadOuterXml());
//Close the reader, then the connection
reader.Close();
conn.Close();
//Await user input.
Console.ReadLine();
}
下面的链接说,从SSAS多维数据集中检索数据的最快方法是XMLReader
拥有XmlReader后,您可能需要一个单元集来读取数据和元数据 看 在“在断开连接的状态下检索数据”一节中
字符串演示DisconnectedCellSet(){
//创建一个新的字符串生成器来存储结果
System.Text.StringBuilder结果=新建System.Text.StringBuilder();
//连接到本地服务器
使用(AdomdConnection conn=new AdomdConnection(“数据源=localhost;”))
{
conn.Open();
//使用此连接创建命令
AdomdCommand cmd=conn.CreateCommand();
cmd.CommandText=@”
成员[措施][FreightCostPerOrder]为
[措施].[经销商运费]/[措施].[经销商订单数量],
格式\字符串='货币'
挑选
【地理】【地理】【国家】、【美国】。孩子们坐在一排,
列上的[日期][日历][日历年]
来自[冒险作品]
其中[措施][运费订单]”;
//执行查询,返回一个XmlReader
System.Xml.XmlReader x=cmd.ExecuteXmlReader();
//此时,XmlReader可以存储在磁盘上,
//传输、修改、缓存或以其他方式操纵
//使用指定的XML加载单元集
CellSet cs=CellSet.LoadXml(x);
//现在XmlReader已经完成了读取
//我们可以关闭它和连接,而
//单元集可以继续使用。
x、 Close();
康涅狄格州关闭();
//从第一个轴输出列标题
//请注意,此过程假定每列存在一个成员。
结果。追加(“\t”);
TupleCollection tuplesOnColumns=cs.Axes[0].Set.Tuples;
foreach(tuplesOnColumns中的Tuple列)
{
result.Append(column.Members[0]。标题+“\t”);
}
result.AppendLine();
//从第二个轴和单元格数据输出行标题
//请注意,此过程假定为二维单元集
TupleCollection tuplesOnRows=cs.Axes[1].Set.Tuples;
for(int row=0;row
您已从中复制了此代码。你设置好数据库了吗?reader.ReadOuterXml()返回什么?我想知道在不知道查询将返回多少列的情况下如何检索数据。是的,那么reader.ReadOuterXml()返回什么?
string DemonstrateDisconnectedCellset() {
//Create a new string builder to store the results
System.Text.StringBuilder result = new System.Text.StringBuilder();
//Connect to the local server
using (AdomdConnection conn = new AdomdConnection("Data Source=localhost;"))
{
conn.Open();
//Create a command, using this connection
AdomdCommand cmd = conn.CreateCommand();
cmd.CommandText = @"
WITH MEMBER [Measures].[FreightCostPerOrder] AS
[Measures].[Reseller Freight Cost]/[Measures].[Reseller Order Quantity],
FORMAT_STRING = 'Currency'
SELECT
[Geography].[Geography].[Country].&[United States].Children ON ROWS,
[Date].[Calendar].[Calendar Year] ON COLUMNS
FROM [Adventure Works]
WHERE [Measures].[FreightCostPerOrder]";
//Execute the query, returning an XmlReader
System.Xml.XmlReader x = cmd.ExecuteXmlReader();
//At this point, the XmlReader could be stored on disk,
//transmitted, modified, cached, or otherwise manipulated
//Load the CellSet with the specified XML
CellSet cs = CellSet.LoadXml(x);
//Now that the XmlReader has finished being read
//we can close it and the connection, while the
//CellSet can continue being used.
x.Close();
conn.Close();
//Output the column captions from the first axis
//Note that this procedure assumes a single member exists per column.
result.Append("\t");
TupleCollection tuplesOnColumns = cs.Axes[0].Set.Tuples;
foreach (Tuple column in tuplesOnColumns)
{
result.Append(column.Members[0].Caption + "\t");
}
result.AppendLine();
//Output the row captions from the second axis and cell data
//Note that this procedure assumes a two-dimensional cellset
TupleCollection tuplesOnRows = cs.Axes[1].Set.Tuples;
for (int row = 0; row < tuplesOnRows.Count; row++)
{
result.Append(tuplesOnRows[row].Members[0].Caption + "\t");
for (int col = 0; col < tuplesOnColumns.Count; col++)
{
result.Append(cs.Cells[col, row].FormattedValue + "\t");
}
result.AppendLine();
}
return result.ToString();
} // using connection
}