我的本地计算机上有一个SQL Server实例,名为.\SC
。我想使用PowerShell脚本从该实例中删除数据库。我需要用sa
用户名登录我的数据库。
这是我到目前为止的代码,但是不起作用:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$srv = new-object Microsoft.SqlServer.Management.Smo.Server(".\SC")
$conContext = $srv.ConnectionContext
$conContext.LoginSecure = $FALSE
$conContext.Login = "sa"
$conContext.Password = "MyPlainTextPass"
$srv2 = new-object Microsoft.SqlServer.Management.Smo.Server($conContext)
$srv2.Databases
最后一行应该列出我的SQL实例中的数据库...但是它给了我这个错误:
尝试枚举集合时发生以下异常:“无法连接到服务器。\ SC。”。在第1行:char:1 + $ srv2.Databases + ~~~~~~~~~~~~~~~ + CategoryInfo:未指定:(:) [],ExtendedTypeSystemException + FullyQualifiedErrorId:ExceptionInGetEnumerator
我究竟做错了什么?
我找到了一个不同的命令来执行此操作。简直是:
invoke-sqlcmd -ServerInstance ".\SC" -U "sa" -P "MyPlainTextPass" -Query "Drop database MyDatabase;"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句