示例#1
0
        public IEnumerable <string> RenameColumn(string tableName, string oldName, string newName)
        {
            Table table  = GetTable(tableName);
            var   column = new Microsoft.SqlServer.Management.Smo.Column(table, oldName);

            // rename default constraint
            string            oldDefaultConstraintName = ObjectNameHelper.GetObjectName(tableName, "DF", MaximumDbObjectNameLength, oldName);
            string            constraintName           = oldDefaultConstraintName;
            DefaultConstraint defaultConstraint        = column.AddDefaultConstraint(constraintName);

            defaultConstraint.Rename(ObjectNameHelper.GetObjectName(tableName, "DF", MaximumDbObjectNameLength, newName));

            // rename column
            table.Columns.Add(column);
            column.Rename(newName);

            // script changes
            IEnumerable <string> commandTexts         = ScriptChanges(table.Parent.Parent);
            string renameDefaultConstraintCommandText = string.Format(CultureInfo.InvariantCulture, "IF OBJECT_ID('{0}') IS NOT NULL ", oldDefaultConstraintName) + commandTexts.First();

            yield return(Regex.Replace(renameDefaultConstraintCommandText, @"EXEC \[\w+\]\.dbo\.sp_rename", @"EXEC dbo.sp_rename")); // for some reason SMO is putting the server name in front of dbo.sp_rename which we do not have in the SqlServerProvider

            foreach (string commandText in commandTexts.Skip(1))
            {
                yield return(commandText);
            }
        }
示例#2
0
        private static DefaultConstraint AddDefaultConstraint(Microsoft.SqlServer.Management.Smo.Column column, string tableName, object value)
        {
            string            constraintName    = ObjectNameHelper.GetObjectName(tableName, "DF", MaximumDbObjectNameLength, column.Name);
            DefaultConstraint defaultConstraint = column.AddDefaultConstraint(constraintName);

            defaultConstraint.Text = GetDefaultValueAsString(value);
            return(defaultConstraint);
        }
示例#3
0
        public IEnumerable <string> DropColumn(string tableName, string columnName)
        {
            Table  table = GetTable(tableName);
            string defaultConstraintName = ObjectNameHelper.GetObjectName(tableName, "DF", MaximumDbObjectNameLength, columnName);
            var    column = new Microsoft.SqlServer.Management.Smo.Column(table, columnName);

            column.AddDefaultConstraint(defaultConstraintName);
            table.Columns.Add(column);

            column.Drop();

            // script changes
            IEnumerable <string> commandTexts       = ScriptChanges(table.Parent.Parent);
            string dropDefaultConstraintCommandText = string.Format(CultureInfo.InvariantCulture, "IF OBJECT_ID('{0}') IS NOT NULL ", defaultConstraintName) + commandTexts.First();

            yield return(dropDefaultConstraintCommandText);

            foreach (string commandText in commandTexts.Skip(1))
            {
                yield return(commandText);
            }
        }
示例#4
0
        public IEnumerable<string> RenameColumn(string tableName, string oldName, string newName)
        {
            Table table = GetTable(tableName);
            var column = new Microsoft.SqlServer.Management.Smo.Column(table, oldName);

            // rename default constraint
            string oldDefaultConstraintName = ObjectNameHelper.GetObjectName(tableName, "DF", MaximumDbObjectNameLength, oldName);
            string constraintName = oldDefaultConstraintName;
            DefaultConstraint defaultConstraint = column.AddDefaultConstraint(constraintName);
            defaultConstraint.Rename(ObjectNameHelper.GetObjectName(tableName, "DF", MaximumDbObjectNameLength, newName));

            // rename column
            table.Columns.Add(column);
            column.Rename(newName);

            // script changes
            IEnumerable<string> commandTexts = ScriptChanges(table.Parent.Parent);
            string renameDefaultConstraintCommandText = string.Format(CultureInfo.InvariantCulture, "IF OBJECT_ID('{0}') IS NOT NULL ", oldDefaultConstraintName) + commandTexts.First();
            yield return Regex.Replace(renameDefaultConstraintCommandText, @"EXEC \[\w+\]\.dbo\.sp_rename", @"EXEC dbo.sp_rename"); // for some reason SMO is putting the server name in front of dbo.sp_rename which we do not have in the SqlServerProvider
            foreach (string commandText in commandTexts.Skip(1))
            {
                yield return commandText;
            }
        }
示例#5
0
        public IEnumerable<string> DropColumn(string tableName, string columnName)
        {
            Table table = GetTable(tableName);
            string defaultConstraintName = ObjectNameHelper.GetObjectName(tableName, "DF", MaximumDbObjectNameLength, columnName);
            var column = new Microsoft.SqlServer.Management.Smo.Column(table, columnName);
            column.AddDefaultConstraint(defaultConstraintName);
            table.Columns.Add(column);

            column.Drop();

            // script changes
            IEnumerable<string> commandTexts = ScriptChanges(table.Parent.Parent);
            string dropDefaultConstraintCommandText = string.Format(CultureInfo.InvariantCulture, "IF OBJECT_ID('{0}') IS NOT NULL ", defaultConstraintName) + commandTexts.First();
            yield return dropDefaultConstraintCommandText;
            foreach (string commandText in commandTexts.Skip(1))
            {
                yield return commandText;
            }
        }