2

我在创建维度属性时遇到错误,我想附加更多属性,但它给出了异常。

#region 创建一个创建维度、属性、层次结构和成员属性对象。/// 创建维度、属性、层次结构和成员属性对象。

    private static object[] CreateDimension(Database objDatabase, DataSourceView objDataSourceView, string[,] strTableNamesAndKeys, int intDimensionTableCount)
    {
        try
        {
            Console.WriteLine("Creating the Dimension, Attribute, Hierarchy, and MemberProperty Objects ...");


            Dimension[] objDimensions = new Dimension[intDimensionTableCount];
            for (int i = 0; i < intDimensionTableCount; i++)
            {
                objDimensions[i] = (Dimension)GenerateDimension(objDatabase, objDataSourceView, strTableNamesAndKeys[i, 0], strTableNamesAndKeys[i, 1]);
            }


            return objDimensions;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error in Creating the Dimension, Attribute, Hierarchy, and MemberProperty Objects. Error Message -> " + ex.Message);
            return null;
        }
    }
    /// Generate single dimension.

    static DataItem CreateDataItem(DataSourceView dsv, string tableName, string columnName)
    {
        DataTable dataTable = ((DataSourceView)dsv).Schema.Tables[tableName];
        DataColumn dataColumn = dataTable.Columns[columnName];
        return new DataItem(tableName, columnName,
            OleDbTypeConverter.GetRestrictedOleDbType(dataColumn.DataType));
    }
    private static object GenerateDimension(Database objDatabase, DataSourceView objDataSourceView, string strTableName, string strTableKeyName)
    {
        try
        {
            Dimension objDimension = new Dimension();

            //Add Dimension to the Database
            objDimension = objDatabase.Dimensions.Add(strTableName);
            objDimension.Source = new DataSourceViewBinding(objDataSourceView.ID);
            DimensionAttributeCollection objDimensionAttributesColl = objDimension.Attributes;
            //Add Dimension Attributes
            DimensionAttribute objAttribute = objDimensionAttributesColl.Add(strTableKeyName);
            //Set Attribute usage and source
            objAttribute.Usage = AttributeUsage.Key;
            objAttribute.KeyColumns.Add(strTableName, strTableKeyName, OleDbType.Integer);

            objDimension.Update();

            if (strTableName == "DimProduct")
            {
          //  objDimension = objDatabase.Dimensions.Add(strTableName);
                Dimension dim;
                DimensionAttribute attr;

                dim = objDatabase.Dimensions.GetByName("DimProduct");
                attr = dim.Attributes.Add("Weight");
                attr.KeyColumns.Add(CreateDataItem(objDatabase.DataSourceViews[0], "DimProduct", "Weight"));
                attr.AttributeHierarchyEnabled = false;

                objDatabase.Update();

            }
            return objDimension;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error in Creating the Dimension, Attribute, Hierarchy, and MemberProperty Objects - GenerateDimension. Error Message -> " + ex.Message);
            return null;
        }
    }
    #endregion Creating a Creating the Dimension, Attribute, Hierarchy, and MemberProperty Objects.

我应该怎么办?

4

0 回答 0