コード例 #1
0
ファイル: SqlTableCollection.cs プロジェクト: locbet/sneal
        /// <summary>
        /// Adds a new table to the database with specified columns.
        /// </summary>
        /// <param name="name">
        /// The name of the table to create.
        /// </param>
        /// <param name="columnInfos">
        /// An array of at least one SqlColumnInformation object with the column information filled out.
        /// </param>
        /// <returns>
        /// If the operation succeeded, the return value is the table created.
        /// </returns>
        public SqlTable Add(string name, SqlColumnInformation[] columnInfos)
        {
            if (name == null || name.Length == 0)
            {
                throw new ArgumentException(SR.GetString("SqlTableCollection_MustHaveValidName"));
            }

            if (this[name] != null)
            {
                throw new ArgumentException(String.Format(SR.GetString("SqlTableCollection_NameAlreadyExists"), name));
            }

            if (columnInfos == null || columnInfos.Length == 0)
            {
                throw new ArgumentException(SR.GetString("SqlTableCollection_AtLeastOneColumn"));
            }

            // Create new table
            NativeMethods.ITable dmoTable = (NativeMethods.ITable) new NativeMethods.Table();
            dmoTable.SetName(name);


            // No need to clear out keys since this is a new table
            // Create new primary key with list of columns
            NativeMethods.IKey key = (NativeMethods.IKey) new NativeMethods.Key();
            key.SetType(NativeMethods.SQLDMO_KEY_TYPE.SQLDMOKey_Primary);


            // Add columns to table
            for (int i = 0; i < columnInfos.Length; i++)
            {
                NativeMethods.IColumn dmoColumn = (NativeMethods.IColumn) new NativeMethods.Column();

                dmoColumn.SetName(columnInfos[i].Name);
                dmoColumn.SetDatatype(columnInfos[i].DataType);
                dmoColumn.SetLength(columnInfos[i].Size);
                dmoColumn.SetAllowNulls(columnInfos[i].Nulls);
                dmoColumn.SetNumericPrecision(columnInfos[i].Precision);
                dmoColumn.SetNumericScale(columnInfos[i].Scale);
                dmoColumn.SetIdentity(columnInfos[i].Identity);
                dmoColumn.SetIdentitySeed(columnInfos[i].IdentitySeed);
                dmoColumn.SetIdentityIncrement(columnInfos[i].IdentityIncrement);
                dmoColumn.SetIsRowGuidCol(columnInfos[i].IsRowGuid);

                // According to SQL Server Books Online, a name for this default will be generated automatically
                dmoColumn.GetDRIDefault().SetText(columnInfos[i].DefaultValue);

                // Add the column on the DMO side
                dmoTable.GetColumns().Add(dmoColumn);

                // If this column is in the primary key, add it to the primary key column list
                if (columnInfos[i].Key)
                {
                    key.GetKeyColumns().Add(columnInfos[i].Name);
                }
            }


            // If there is anything in the primary key, add it
            if (key.GetKeyColumns().GetCount() > 0)
            {
                dmoTable.GetKeys().Add(key);
            }


            // Add table to database
            database.dmoDatabase.GetTables().Add(dmoTable);


            database.Server.Databases.Refresh();
            this.Refresh();

            return(this[name]);
        }