Share via


Criando, alterando e removendo padrões

No SQL Server Management Objects (SMO), a restrição padrão é representada pelo objeto Default.

A propriedade TextBody do objeto Default é usada para definir o valor a ser inserido. Ela pode ser uma constante ou uma instrução Transact-SQL que retorna um valor constante, como GETDATE(). A propriedade TextBody não pode ser modificada usando o método Alter. Em vez disso, o objeto Default deve ser descartado e recriado.

Exemplo

Para usar qualquer exemplo de código fornecido, será necessário escolher o ambiente de programação, o modelo de programação e a linguagem de programação para criar o seu aplicativo. Para obter mais informações, consulte Como criar um projeto SMO do Visual Basic no Visual Studio .NET ou Como criar um projeto SMO do Visual C# no Visual Studio .NET.

Criando, alterando e removendo um padrão no Visual Basic

Este exemplo de código mostra como criar um padrão que seja texto simples e outro padrão que seja uma instrução Transact-SQL. O padrão deve ser anexado à coluna usando o método BindToColumn e desanexado usando o método UnbindFromColumn.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'Define a Default object variable by supplying the parent database and the default name 
'in the constructor.
Dim def As [Default]
def = New [Default](db, "Test_Default2")
'Set the TextHeader and TextBody properties that define the default.
def.TextHeader = "CREATE DEFAULT [Test_Default2] AS"
def.TextBody = "GetDate()"
'Create the default on the instance of SQL Server.
def.Create()
'Declare a Column object variable and reference a column in the AdventureWorks2008R2 database.
Dim col As Column
col = db.Tables("SpecialOffer", "Sales").Columns("StartDate")
'Bind the default to the column.
def.BindToColumn("SpecialOffer", "StartDate", "Sales")
'Unbind the default from the column and remove it from the database.
def.UnbindFromColumn("SpecialOffer", "StartDate", "Sales")
def.Drop()

Criando, alterando e removendo um padrão no Visual C#

Este exemplo de código mostra como criar um padrão que seja texto simples e outro padrão que seja uma instrução Transact-SQL. O padrão deve ser anexado à coluna através do método BindToColumn e desanexado através do método UnbindFromColumn.

{
          
          Server srv = new Server();

            //Reference the AdventureWorks2008R2 database. 
            Database  db = srv.Databases["AdventureWorks2008R2"];

            //Define a Default object variable by supplying the parent database and the default name 
            //in the constructor. 
            Default def = new Default(db, "Test_Default2");

            //Set the TextHeader and TextBody properties that define the default. 
            def.TextHeader = "CREATE DEFAULT [Test_Default2] AS";
            def.TextBody = "GetDate()";

            //Create the default on the instance of SQL Server. 
            def.Create();
            
            //Bind the default to a column in a table in AdventureWorks2008R2
            def.BindToColumn("SpecialOffer", "StartDate", "Sales");

            //Unbind the default from the column and remove it from the database. 
            def.UnbindFromColumn("SpecialOffer", "StartDate", "Sales");
            def.Drop();
        }

Criando, alterando e removendo um padrão no PowerShell

Este exemplo de código mostra como criar um padrão que seja texto simples e outro padrão que seja uma instrução Transact-SQL. O padrão deve ser anexado à coluna através do método BindToColumn e desanexado através do método UnbindFromColumn.

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2008R2
CD \sql\localhost\default\databases
$db = get-item Adventureworks2008R2

#Define a Default object variable by supplying the parent database and the default name in the constructor.
$def = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Default `
-argumentlist $db, "Test_Default2"

#Set the TextHeader and TextBody properties that define the default. 
$def.TextHeader = "CREATE DEFAULT [Test_Default2] AS"
$def.TextBody = "GetDate()"

#Create the default on the instance of SQL Server. 
$def.Create()

#Bind the default to the column. 
$def.BindToColumn("SpecialOffer", "StartDate", "Sales")
          
#Unbind the default from the column and remove it from the database. 
$def.UnbindFromColumn("SpecialOffer", "StartDate", "Sales")
$def.Drop()

Consulte também

Referência