AddUniqueKey() private method

private AddUniqueKey ( ) : DataColumn
return DataColumn
 internal DataTable InstantiateTable(DataSet dataSet, XmlElement node, XmlElement typeNode)
 {
     DataTable table;
     string name = "";
     XmlAttributeCollection attrs = node.Attributes;
     int minOccurs = 1;
     int maxOccurs = 1;
     string str2 = null;
     ArrayList tableChildren = new ArrayList();
     if (attrs.Count > 0)
     {
         name = this.GetInstanceName(node);
         table = dataSet.Tables.GetTable(name, this._schemaUri);
         if (table != null)
         {
             return table;
         }
     }
     table = new DataTable(XmlConvert.DecodeName(name)) {
         Namespace = this._schemaUri
     };
     this.GetMinMax(node, ref minOccurs, ref maxOccurs);
     table.MinOccurs = minOccurs;
     table.MaxOccurs = maxOccurs;
     this._ds.Tables.Add(table);
     this.HandleTypeNode(typeNode, table, tableChildren);
     XMLSchema.SetProperties(table, attrs);
     if (str2 != null)
     {
         string[] strArray = str2.TrimEnd(null).Split(null);
         int length = strArray.Length;
         DataColumn[] columnArray = new DataColumn[length];
         for (int i = 0; i < length; i++)
         {
             DataColumn column2 = table.Columns[strArray[i], this._schemaUri];
             if (column2 == null)
             {
                 throw ExceptionBuilder.ElementTypeNotFound(strArray[i]);
             }
             columnArray[i] = column2;
         }
         table.PrimaryKey = columnArray;
     }
     foreach (DataTable table2 in tableChildren)
     {
         DataRelation relation = null;
         DataRelationCollection childRelations = table.ChildRelations;
         for (int j = 0; j < childRelations.Count; j++)
         {
             if (childRelations[j].Nested && (table2 == childRelations[j].ChildTable))
             {
                 relation = childRelations[j];
             }
         }
         if (relation == null)
         {
             DataColumn parentKey = table.AddUniqueKey();
             DataColumn childColumn = table2.AddForeignKey(parentKey);
             relation = new DataRelation(table.TableName + "_" + table2.TableName, parentKey, childColumn, true) {
                 CheckMultipleNested = false,
                 Nested = true
             };
             table2.DataSet.Relations.Add(relation);
             relation.CheckMultipleNested = true;
         }
     }
     return table;
 }
Example #2
0
        internal DataTable InstantiateTable(DataSet dataSet, XmlElement node, XmlElement typeNode)
        {
            string typeName = string.Empty;
            XmlAttributeCollection attrs = node.Attributes;
            DataTable table;
            int minOccurs = 1;
            int maxOccurs = 1;
            string keys = null;
            ArrayList tableChildren = new ArrayList();



            if (attrs.Count > 0)
            {
                typeName = GetInstanceName(node);
                table = dataSet.Tables.GetTable(typeName, _schemaUri);
                if (table != null)
                {
                    return table;
                }
            }

            table = new DataTable(XmlConvert.DecodeName(typeName));
            // fxcop: new DataTable should inherit the CaseSensitive, Locale from DataSet and possibly updating during SetProperties

            table.Namespace = _schemaUri;

            GetMinMax(node, ref minOccurs, ref maxOccurs);
            table.MinOccurs = minOccurs;
            table.MaxOccurs = maxOccurs;

            _ds.Tables.Add(table);

            HandleTypeNode(typeNode, table, tableChildren);

            SetProperties(table, attrs);

            // check to see if we fave unique constraint

            if (keys != null)
            {
                string[] list = keys.TrimEnd(null).Split(null);
                int keyLength = list.Length;

                var cols = new DataColumn[keyLength];

                for (int i = 0; i < keyLength; i++)
                {
                    DataColumn col = table.Columns[list[i], _schemaUri];
                    if (col == null)
                        throw ExceptionBuilder.ElementTypeNotFound(list[i]);
                    cols[i] = col;
                }
                table.PrimaryKey = cols;
            }


            foreach (DataTable _tableChild in tableChildren)
            {
                DataRelation relation = null;

                DataRelationCollection childRelations = table.ChildRelations;

                for (int j = 0; j < childRelations.Count; j++)
                {
                    if (!childRelations[j].Nested)
                        continue;

                    if (_tableChild == childRelations[j].ChildTable)
                        relation = childRelations[j];
                }

                if (relation != null)
                    continue;

                DataColumn parentKey = table.AddUniqueKey();
                // foreign key in the child table
                DataColumn childKey = _tableChild.AddForeignKey(parentKey);
                // create relationship
                // setup relationship between parent and this table
                relation = new DataRelation(table.TableName + "_" + _tableChild.TableName, parentKey, childKey, true);

                relation.CheckMultipleNested = false; // disable the check for multiple nested parent

                relation.Nested = true;
                _tableChild.DataSet.Relations.Add(relation);
                relation.CheckMultipleNested = true; // enable the check for multiple nested parent
            }

            return table;
        }