mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-13 17:23:02 -05:00
This is a documentation-only update so automerging. Please review the commit if there are any follow-ups requested. * Update .gitignore for docfx genertated files * Update documenation (part 1) * Update docs and add some samples * More doc updates
59 lines
2.3 KiB
PowerShell
59 lines
2.3 KiB
PowerShell
<#
|
|
This code example creates a table that has several columns with different types and purposes. The code also provides examples of how to create an identity field, how to create a primary key, and how to alter table properties.
|
|
#>
|
|
|
|
#DLL location needs to be specified
|
|
$pathtodll = ""
|
|
Add-Type -Path "$pathtodll\Microsoft.SqlServer.Smo.dll"
|
|
Add-Type -Path "$pathtodll\Microsoft.SqlServer.ConnectionInfo.dll"
|
|
|
|
#Connection context need to be specified
|
|
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server()
|
|
$srv.ConnectionContext.LoginSecure = $false
|
|
$srv.ConnectionContext.ServerInstance = "instance_name"
|
|
$srv.ConnectionContext.Login = "user_id"
|
|
$srv.ConnectionContext.Password = "pwd"
|
|
|
|
#And the database object corresponding to master.
|
|
$db = $srv.Databases["master"]
|
|
|
|
#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()
|