System.Data.DataTable.AddUniqueKey C# (CSharp) Method

AddUniqueKey() private method

private AddUniqueKey ( ) : DataColumn
return DataColumn
        internal DataColumn AddUniqueKey() => AddUniqueKey(-1);

Same methods

DataTable::AddUniqueKey ( int position ) : DataColumn

Usage Example

Example #1
0
        internal DataTable InstantiateTable(DataSet dataSet, XmlElement node, XmlElement typeNode)
        {
            string typeName = "";
            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;

                DataColumn[] 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);
        }
All Usage Examples Of System.Data.DataTable::AddUniqueKey
DataTable