- 阅读权限
- 255
- 威望
- 1 级
- 论坛币
- 49407 个
- 通用积分
- 51.8704
- 学术水平
- 370 点
- 热心指数
- 273 点
- 信用等级
- 335 点
- 经验
- 57815 点
- 帖子
- 4006
- 精华
- 21
- 在线时间
- 582 小时
- 注册时间
- 2005-5-8
- 最后登录
- 2023-11-26
|
- Creating a database
- This recipe walks through creating a database with default properties using PowerShell.
- Getting ready
- In this example, we are going to create a database called TestDB, and we assume that this database does not yet exist in your instance.
- For your reference, the equivalent T-SQL code of this task is as follows:
- CREATE DATABASE TestDB
- How to do it...
- Follow these steps to create a simple database in SQL Server:
- Open PowerShell ISE as administrator.
- Import the SQLPS module and create a new SMO Server Object:
- #import SQL Server module
- Import-Module SQLPS -DisableNameChecking
- #replace this with your instance name
- $instanceName = "localhost"
- $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
- Add the following script and run:
- #database TestDB with default settings
- #assumption is that this database does not yet exist
- $dbName = "TestDB"
- $db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database($server, $dbName)
- $db.Create()
- #to confirm, list databases in your instance
- $server.Databases |
- Select Name, Status, Owner, CreateDate
复制代码
|
|