是否可以使用 powershell 在 SQL Server 中创建数据库? [英] Is it possible to create a Database in SQL Server with powershell?

查看:59
本文介绍了是否可以使用 powershell 在 SQL Server 中创建数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 powershell 和 SMO 在 SQL 服务器中创建一个空数据库,但似乎找不到这样做的方法.这可能吗?

I am trying to create a empty database in SQL server using powershell and SMO but cannot seem to find a way of doing it. Is this possible?

sql server 的连接脚本:

Connection script for sql server:

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null

$serverName = "localhost"

$server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $serverName

$server.ConnectionContext.LoginSecure=$false;
$credential = Get-Credential
$loginName = $credential.UserName -replace("\\","")
$server.ConnectionContext.set_Login($loginName);
$server.ConnectionContext.set_SecurePassword($credential.Password)
$server.ConnectionContext.ApplicationName="SQLDeploymentScript"

推荐答案

SMO 当然支持 数据库创建方法 - 链接的 MSDN 页面包括一个 powershell 示例:

SMO certainly supports a database create method - the linked MSDN page includes a powershell example:

$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database($srv, "Test_SMO_Database")
$db.Create()
Write-Host $db.CreateDate

这篇关于是否可以使用 powershell 在 SQL Server 中创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆