必要なもの:
Microsoft® SQL Server® 2014 ExpressSSMS まで必要ないなら、PowerShell で管理できるように、
本体:SqlLocalDB.msi と、お好みで 管理用ツールは、SSMS
Microsoft® SQL Server® 2014 Feature Pack
- SharedManagementObjects.msi
- PowerShellTools.msi
Import-Module SQLPS -DisableNameChecking # Create database $QueryString = @" USE master; GO CREATE DATABASE testDB on ( name = 'testDB1', filename='C:\LocalDB_data\testDB.mdf' ) COLLATE Japanese_XJIS_100_CI_AS_KS_WS; "@ Invoke-Sqlcmd $QueryString -ServerInstance '(LocalDB)\MSSQLLocalDB' # Create table $QueryString = @" USE testDB; GO CREATE TABLE Table_1 ( ID int IDENTITY(1,1) NOT NULL ,F_Num int ,F_Date date ,CONSTRAINT PK_Table_1 PRIMARY KEY (ID ASC) ) "@ Invoke-Sqlcmd $QueryString -ServerInstance '(LocalDB)\MSSQLLocalDB' # Drop database Invoke-Sqlcmd "DROP DATABASE testdb" -ServerInstance "(localdb)\MSSQLLocalDB"照合順序を Japanese_XJIS_100_CI_AS_KS_WS にしているのは、Access アプリに合わせているから。
"MSSQLLocalDB"は自動インスタンス名。試すぐらいならこのままでも困ることはない。
コマンド ライン管理ツール: SqlLocalDB.exe
DSNはこんな感じで、(localdb)\インスタンス名 |
Dim tdf As DAO.TableDef Set tdf = CurrentDb.CreateTableDef("Table_1") tdf.Connect = "ODBC;" _ & "Driver={ODBC Driver 11 for SQL Server};" _ & "Server=(localdb)\MSSQLLocalDB;" _ & "Database=testDB;" _ & "Trusted_Connection=yes;" tdf.SourceTableName = "dbo.Table_1" CurrentDb.TableDefs.Append tdf Application.RefreshDatabaseWindow
Import-Module SQLPS -DisableNameChecking $srv = New-Object Microsoft.SqlServer.Management.Smo.Server "(localdb)\mssqllocaldb" $db_new = New-Object Microsoft.SqlServer.Management.Smo.Database $srv,"testDB" $db_new.Collation = 'Japanese_XJIS_100_CI_AS_KS_WS' $fg_p = New-Object Microsoft.SqlServer.Management.Smo.FileGroup $db_new,"PRIMARY" $db_new.FileGroups.Add($fg_p) $df = New-Object Microsoft.SqlServer.Management.Smo.DataFile $fg_p,"datafile1" $df.FileName = "C:\LocalDB_Data\testDB1.mdf" $df.Size=1024*3 $df.GrowthType="KB" $df.Growth=1024 $fg_p.Files.Add($df) $db_new.Create() $type_Date = [Microsoft.SqlServer.Management.Smo.DataType]::Date $type_Int = [Microsoft.SqlServer.Management.Smo.DataType]::Int $type_Text = [Microsoft.SqlServer.Management.Smo.DataType]::NVarChar(20) $tbl_new = New-Object Microsoft.SqlServer.Management.Smo.Table $db_new,"Table_1" $col_1 = New-Object Microsoft.SqlServer.Management.Smo.Column $tbl_new,"ID",$type_Int $col_1.Identity = $true $col_1.IdentitySeed = 1 $col_1.IdentityIncrement = 1 $tbl_new.Columns.Add($col_1) $col_2 = New-Object Microsoft.SqlServer.Management.Smo.Column $tbl_new,"F_Num",$type_Int $col_2.Nullable = $true $tbl_new.Columns.Add($col_2) $col_3 = New-Object Microsoft.SqlServer.Management.Smo.Column $tbl_new,"F_Date",$type_Date $col_3.Nullable = $true $tbl_new.Columns.Add($col_3) $tbl_new.Create() $idx_PK = New-Object Microsoft.SqlServer.Management.Smo.Index $tbl_new,"PK_Table_1" $icol_PK = New-Object Microsoft.SqlServer.Management.Smo.IndexedColumn $idx_PK,"ID",$false $idx_PK.IndexedColumns.Add($icol_PK) $idx_PK.IndexKeyType = [Microsoft.SqlServer.Management.Smo.IndexKeyType]::DriPrimaryKey $idx_PK.IsClustered = $true $idx_PK.Create() $tbl_new.Alter() $db_new.Alter()
0 件のコメント:
コメントを投稿