透视是一种机制,用于简化或将模型聚焦到客户端应用程序的较小部分。
有关如何创建和作透视表示形式的详细说明 ,请参阅透视表示形式(表格 )。
警告
透视不是安全机制;用户仍可通过其他接口访问透视之外的对象。
透视表示形式
就 AMO 对象而言,透视表示形式具有一对一的映射关系 Perspective ,不需要其他主要 AMO 对象。
AMO 中的透视
以下代码片段演示如何在表格模型中创建透视。 此代码片段中的关键元素是 perspectiveElements;此对象是表格模型中向用户公开的所有对象的图形表示形式。 perspectiveElements 包含 4 列,对于此方案,只有 1、2 和 3 列是相关的。 列 1 包含显示的元素类型 -elementTypeValue-;列 2 包含元素的完整名称--,可能需要分析才能在透视中输入元素;第 3 列包含一个复选框项 -checkedElement,指示元素是否是透视的一部分。
private void updatePerspective_Click(
AMO.Cube modelCube
, DataGridView perspectiveElements
, string updatedPerspectiveID
)
{
//Update is done by delete first, create new and insert after
//if perspective doesn't exist then create first and insert after
updatedPerspectiveID = updatedPerspectiveID.Trim();
if (modelCube.Perspectives.Contains(updatedPerspectiveID))
{
modelCube.Perspectives.Remove(updatedPerspectiveID, true);
newDatabase.Update(AMO.UpdateOptions.ExpandFull, AMO.UpdateMode.UpdateOrCreate);
}
AMO.Perspective currentPerspective = modelCube.Perspectives.Add(updatedPerspectiveID, updatedPerspectiveID);
foreach (DataGridViewRow currentDGVRow in perspectiveElements.Rows)
{
bool checkedElement = (bool)currentDGVRow.Cells[3].Value;
if (checkedElement)
{
string elementTypeValue = currentDGVRow.Cells[1].Value.ToString();
string elementNameValue = currentDGVRow.Cells[2].Value.ToString();
switch (elementTypeValue)
{
case "Column: Attribute":
case "Column: Calculated Column":
{
string perspectiveDimensionName = Regex.Match(elementNameValue, @"(?<=')(.*?)(?=')").ToString();
string perspectiveDimensionAttributeName = Regex.Match(elementNameValue, @"(?<=\[)(.*?)(?=\])").ToString();
AMO.PerspectiveDimension currentPerspectiveDimension;
if (!currentPerspective.Dimensions.Contains(perspectiveDimensionName))
{
currentPerspectiveDimension = currentPerspective.Dimensions.Add(perspectiveDimensionName);
}
else
{
currentPerspectiveDimension = currentPerspective.Dimensions[perspectiveDimensionName];
}
if (!currentPerspectiveDimension.Attributes.Contains(perspectiveDimensionAttributeName))
{
currentPerspectiveDimension.Attributes.Add(perspectiveDimensionAttributeName);
}
}
break;
case "Hierarchy":
{
string perspectiveDimensionName = Regex.Match(elementNameValue, @"(?<=')(.*?)(?=')").ToString();
string perspectiveDimensionHierarchyName = Regex.Match(elementNameValue, @"(?<=\[)(.*?)(?=\])").ToString();
AMO.PerspectiveDimension currentPerspectiveDimension;
if (!currentPerspective.Dimensions.Contains(perspectiveDimensionName))
{
currentPerspectiveDimension = currentPerspective.Dimensions.Add(perspectiveDimensionName);
}
else
{
currentPerspectiveDimension = currentPerspective.Dimensions[perspectiveDimensionName];
}
if (!currentPerspectiveDimension.Hierarchies.Contains(perspectiveDimensionHierarchyName))
{
currentPerspectiveDimension.Hierarchies.Add(perspectiveDimensionHierarchyName);
}
}
break;
case "Measure":
{
string perspectiveMeasureGroupName = Regex.Match(elementNameValue, @"(?<=')(.*?)(?=')").ToString();
string measureName = Regex.Match(elementNameValue, @"(?<=\[)(.*?)(?=\])").ToString();
string perspectiveMeasureName = string.Format("[Measures].[{0}]", measureName);
AMO.PerspectiveCalculation currentPerspectiveCalculation = new AMO.PerspectiveCalculation(perspectiveMeasureName, AMO.PerspectiveCalculationType.Member);
if (!currentPerspective.Calculations.Contains(perspectiveMeasureName))
{
currentPerspective.Calculations.Add(currentPerspectiveCalculation);
}
if (modelCube.MdxScripts["MdxScript"].CalculationProperties.Contains(string.Format("KPIs.[{0}]", measureName)))
{//Current Measure is also a KPI ==> will be added to the KPIs collection of the perspective
AMO.PerspectiveKpi currentPerspectiveKpi = new AMO.PerspectiveKpi(perspectiveMeasureName);
if (!currentPerspective.Kpis.Contains(perspectiveMeasureName))
{
currentPerspective.Kpis.Add(currentPerspectiveKpi);
}
}
}
break;
default:
break;
}
}
}
newDatabase.Update(AMO.UpdateOptions.ExpandFull, AMO.UpdateMode.CreateOrReplace);
MessageBox.Show(String.Format("Perpective successfully updated."), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
AMO2Tabular 示例
但是,若要了解如何使用 AMO 创建和作透视表示形式,请参阅 AMO 到表格示例的源代码。 该示例在 Codeplex 中提供。 有关代码的重要说明:代码仅作为对此处介绍的逻辑概念的支持提供,不应在生产环境中使用;它也不应用于除教学以外的其他用途。