Aracılığıyla paylaş


Oluşturma, değiştirme ve varsayılan kaldırma

De SQL ServerManagement Objects (smo), varsayılan kısıtlama olarak temsil Defaultnesnesini.

TextBodyÖzelliği Defaultnesne eklenecek değerini ayarlamak için kullanılır. Bu sabit olabilir ya da bir Transact-SQLgetdate() gibi sabit bir değer döndüren deyim. TextBodyÖzelliği kullanılarak değiştirilemez Alteryöntemi. Bunun yerine, Defaultnesne atılan ve atılacak.

Örnek

Sunulan kod örneklerinden herhangi birini kullanmak için, programlama ortamını, programlama şablonunu ve uygulamanızı oluşturacağınız programlama dilini seçmeniz gerekecektir. Daha fazla bilgi için, bkz. Visual Studio'da Visual Basic smo proje oluşturun.NET veya Visual Studio'da Visual C# smo proje oluşturun.NET.

Oluşturma, değiştirme ve Visual Basic'te varsayılan kaldırma

Bu kod örneği nasıl basit bir metin olan tek bir varsayılan ve olan başka bir varsayılan oluşturmak için gösterir bir Transact-SQLdeyimi. Varsayılan sütun kullanarak bağlı olmalıdır BindToColumnyöntemi ve kullanarak müstakil UnbindFromColumnyöntemi.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2012 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'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 AdventureWorks2012 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()

Oluşturma, değiştirme ve varsayılan Visual C# içinde kaldırma

Bu kod örneği nasıl basit bir metin olan tek bir varsayılan ve olan başka bir varsayılan oluşturmak için gösterir bir Transact-SQLdeyimi. Varsayılan sütun kullanarak bağlı olmalıdır BindToColumnyöntemi ve kullanarak müstakil UnbindFromColumnyöntemi.

{
          
          Server srv = new Server();

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

            //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 AdventureWorks2012
            def.BindToColumn("SpecialOffer", "StartDate", "Sales");

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

{
          
          Server srv = new Server();

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

            //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 AdventureWorks2012
            def.BindToColumn("SpecialOffer", "StartDate", "Sales");

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

Oluşturma, değiştirme ve PowerShell varsayılan kaldırma

Bu kod örneği nasıl basit bir metin olan tek bir varsayılan ve olan başka bir varsayılan oluşturmak için gösterir bir Transact-SQLdeyimi. Varsayılan sütun kullanarak bağlı olmalıdır BindToColumnyöntemi ve kullanarak müstakil UnbindFromColumnyöntemi.

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

#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()

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

#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()

Ayrıca bkz.

Başvuru

Default