Aracılığıyla paylaş


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

De SQL ServerManagement Objects (smo), tabloları gösterilir Tablenesnesini. smo nesne hiyerarşisinde, Tablenesnedir aşağıda Databasenesnesini.

Örnek

Sağlanan herhangi bir kod örneği kullanmak için programlama ortamı, şablon ve uygulamanız oluşturulacağı dili seçmek zorundasınız. 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 bir tablo kaldırma

Bu kod örneği, farklı türleri ve amaçları ile birden fazla sütun içeren bir tablo oluşturur. Kodu da nasıl bir kimlik alanı oluşturmak için bir birincil anahtar oluşturmak nasıl ve tablo özelliklerini değiştirmek nasıl örnekleri sağlar.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2012 2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'Define a Table object variable by supplying the parent database and table name in the constructor. 
Dim tb As Table
tb = New Table(db, "Test_Table")
'Add various columns to the table.
Dim col1 As Column
col1 = New Column(tb, "Name", DataType.NChar(50))
col1.Collation = "Latin1_General_CI_AS"
col1.Nullable = True
tb.Columns.Add(col1)
Dim col2 As Column
col2 = New Column(tb, "ID", DataType.Int)
col2.Identity = True
col2.IdentitySeed = 1
col2.IdentityIncrement = 1
tb.Columns.Add(col2)
Dim col3 As Column
col3 = New Column(tb, "Value", DataType.Real)
tb.Columns.Add(col3)
Dim col4 As Column
col4 = New Column(tb, "Date", DataType.DateTime)
col4.Nullable = False
tb.Columns.Add(col4)
'Create the table on the instance of SQL Server.
tb.Create()
'Add another column.
Dim col5 As Column
col5 = New Column(tb, "ExpiryDate", DataType.DateTime)
col5.Nullable = False
tb.Columns.Add(col5)
'Run the Alter method to make the change on the instance of SQL Server.
tb.Alter()
'Remove the table from the database.

tb.Drop()

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

Bu kod örneği, farklı türleri ve amaçları ile birden fazla sütun içeren bir tablo oluşturur. Kodu da nasıl bir kimlik alanı oluşturmak için bir birincil anahtar oluşturmak nasıl ve tablo özelliklerini değiştirmek nasıl örnekleri sağlar.

{
            //Connect to the local, default instance of SQL Server. 
        Server srv; 
        srv = new Server(); 
        //Reference the AdventureWorks2012 database. 
        Database db; 
        db = srv.Databases["AdventureWorks2012"]; 
        //Define a Table object variable by supplying the parent database and table name in the constructor. 
        Table tb; 
        tb = new Table(db, "Test_Table"); 
        //Add various columns to the table. 
        Column col1; 
        col1 = new Column(tb, "Name", DataType.NChar(50)); 
        col1.Collation = "Latin1_General_CI_AS"; 
        col1.Nullable = true; 
        tb.Columns.Add(col1); 
        Column col2; 
        col2 = new Column(tb, "ID", DataType.Int); 
        col2.Identity = true; 
        col2.IdentitySeed = 1; 
        col2.IdentityIncrement = 1; 
        tb.Columns.Add(col2); 
        Column col3; 
        col3 = new Column(tb, "Value", DataType.Real); 
        tb.Columns.Add(col3); 
        Column col4; 
        col4 = new Column(tb, "Date", DataType.DateTime); 
        col4.Nullable = false; 
        tb.Columns.Add(col4); 
        //Create the table on the instance of SQL Server. 
        tb.Create(); 
        //Add another column. 
        Column col5; 
        col5 = new Column(tb, "ExpiryDate", DataType.DateTime); 
        col5.Nullable = false; 
        tb.Columns.Add(col5); 
        //Run the Alter method to make the change on the instance of SQL Server. 
        tb.Alter(); 
        //Remove the table from the database. 
        tb.Drop(); 
        }

{
            //Connect to the local, default instance of SQL Server. 
        Server srv; 
        srv = new Server(); 
        //Reference the AdventureWorks2012 database. 
        Database db; 
        db = srv.Databases["AdventureWorks2012"]; 
        //Define a Table object variable by supplying the parent database and table name in the constructor. 
        Table tb; 
        tb = new Table(db, "Test_Table"); 
        //Add various columns to the table. 
        Column col1; 
        col1 = new Column(tb, "Name", DataType.NChar(50)); 
        col1.Collation = "Latin1_General_CI_AS"; 
        col1.Nullable = true; 
        tb.Columns.Add(col1); 
        Column col2; 
        col2 = new Column(tb, "ID", DataType.Int); 
        col2.Identity = true; 
        col2.IdentitySeed = 1; 
        col2.IdentityIncrement = 1; 
        tb.Columns.Add(col2); 
        Column col3; 
        col3 = new Column(tb, "Value", DataType.Real); 
        tb.Columns.Add(col3); 
        Column col4; 
        col4 = new Column(tb, "Date", DataType.DateTime); 
        col4.Nullable = false; 
        tb.Columns.Add(col4); 
        //Create the table on the instance of SQL Server. 
        tb.Create(); 
        //Add another column. 
        Column col5; 
        col5 = new Column(tb, "ExpiryDate", DataType.DateTime); 
        col5.Nullable = false; 
        tb.Columns.Add(col5); 
        //Run the Alter method to make the change on the instance of SQL Server. 
        tb.Alter(); 
        //Remove the table from the database. 
        tb.Drop(); 
        }

Oluşturma, değiştirme ve PowerShell içinde bir tablo kaldırma

Bu kod örneği, farklı türleri ve amaçları ile birden fazla sütun içeren bir tablo oluşturur. Kodu da nasıl bir kimlik alanı oluşturmak için bir birincil anahtar oluşturmak nasıl ve tablo özelliklerini değiştirmek nasıl örnekleri sağlar.

#Load the assembly containing the objects used in this example
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Types")

# Set the path context to the local, default instance of SQL Server.
CD \sql\localhost\default\Databases\

#And the database object corresponding to AdventureWorks2012.
$db = get-item AdventureWorks2012

#Create a SMO Table
$tb = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Table -argumentlist $db, "Test_Table"

#Add various columns to the table. 
$Type = [Microsoft.SqlServer.Management.SMO.DataType]::NChar(50)
$col1 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"Name", $Type
$col1.Collation = "Latin1_General_CI_AS"
$col1.Nullable = $true
$tb.Columns.Add($col1)

$Type = [Microsoft.SqlServer.Management.SMO.DataType]::Int
$col2 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"ID", $Type
$col2.Identity = $true
$col2.IdentitySeed = 1
$col2.IdentityIncrement = 1
$tb.Columns.Add($col2) 

$Type = [Microsoft.SqlServer.Management.SMO.DataType]::Real
$col3 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"Value", $Type
$tb.Columns.Add($col3) 

$Type = [Microsoft.SqlServer.Management.SMO.DataType]::DateTime
$col4 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"Date", $Type
$col4.Nullable = $false
$tb.Columns.Add($col4) 

#Create the table
$tb.Create()

$Type = [Microsoft.SqlServer.Management.SMO.DataType]::DateTime
$col5 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"ExpiryDate", $Type
$col5.Nullable = $false
$tb.Columns.Add($col5) 

#Run the Alter method to make the change on the instance of SQL Server. 
$tb.Alter()

#Remove the table from the database. 
$tb.Drop()

#Load the assembly containing the objects used in this example
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Types")

# Set the path context to the local, default instance of SQL Server.
CD \sql\localhost\default\Databases\

#And the database object corresponding to AdventureWorks2012.
$db = get-item AdventureWorks2012

#Create a SMO Table
$tb = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Table -argumentlist $db, "Test_Table"

#Add various columns to the table. 
$Type = [Microsoft.SqlServer.Management.SMO.DataType]::NChar(50)
$col1 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"Name", $Type
$col1.Collation = "Latin1_General_CI_AS"
$col1.Nullable = $true
$tb.Columns.Add($col1)

$Type = [Microsoft.SqlServer.Management.SMO.DataType]::Int
$col2 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"ID", $Type
$col2.Identity = $true
$col2.IdentitySeed = 1
$col2.IdentityIncrement = 1
$tb.Columns.Add($col2) 

$Type = [Microsoft.SqlServer.Management.SMO.DataType]::Real
$col3 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"Value", $Type
$tb.Columns.Add($col3) 

$Type = [Microsoft.SqlServer.Management.SMO.DataType]::DateTime
$col4 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"Date", $Type
$col4.Nullable = $false
$tb.Columns.Add($col4) 

#Create the table
$tb.Create()

$Type = [Microsoft.SqlServer.Management.SMO.DataType]::DateTime
$col5 =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.Column -argumentlist $tb,"ExpiryDate", $Type
$col5.Nullable = $false
$tb.Columns.Add($col5) 

#Run the Alter method to make the change on the instance of SQL Server. 
$tb.Alter()

#Remove the table from the database. 
$tb.Drop()

Ayrıca bkz.

Başvuru

Table