简体   繁体   English

已由Management Studio分离的Powershell附加sql服务器数据库

[英]powershell attach sql server database that has been detached by management studio

I am writing a powershell script that will automate a dev environment deployment and I've hit a problem with attaching the db's. 我正在编写一个Powershell脚本,该脚本将自动执行开发环境部署,并且在附加数据库时遇到了问题。 I am adding the 2 snap ins SqlServerCmdletSnapin100 and SqlServerProviderSnapin100 and using SQLSERVER:\\SQL\\localhost\\SQLEXPRESS and the AttachDatabase method. 我正在添加2个管理单元SqlServerCmdletSnapin100SqlServerProviderSnapin100并使用SQLSERVER:\\SQL\\localhost\\SQLEXPRESSAttachDatabase方法。 This is working well and if I use DetachDatabase method in the same way I can re-run the script continually. 这很好,如果我以相同的方式使用DetachDatabase方法,可以连续重新运行脚本。 My problem arises when I detach from the management studio and try to run the script again. 当我从Management Studio分离并尝试再次运行脚本时,就会出现我的问题。 No matter what I do here (permissions etc.) the script will continually fail from this point on with error: 无论我在这里做什么(权限等),脚本从此刻开始都会连续失败并出现错误:

Exception calling "AttachDatabase" with "2" argument(s): 
"Attach database failed for Server 'localhost\SQLEXPRESS'. "

If I change the name of the database I am attaching as the script will work again. 如果更改数据库名称,则将附加脚本,因为该脚本将再次起作用。 Is there something in a system db that would be hanging onto the Database or database files that I need to remove as well? 系统db中是否有挂在数据库或我也要删除的数据库文件上的东西?

SMO uses nested error objects, so I'm wondering what the base error message states. SMO使用嵌套的错误对象,因此我想知道基本错误消息指出了什么。 If you run this statement: 如果运行此语句:

$error[0] | fl -force

What error message do you get 您得到什么错误信息

Update Ran a quick test: Detach database "hsg" from my local instance using SSMS and successfully attached with this script: Update运行了一个快速测试:使用SSMS从本地实例中分离数据库“ hsg”,并成功附加以下脚本:

PS SQLSERVER:\SQL\WIN7BOOT\SQL1> $s = get-item .
PS SQLSERVER:\SQL\WIN7BOOT\SQL1> $s.AttachDatabase("hsg",$sc)
PS SQLSERVER:\SQL\WIN7BOOT\SQL1> $sc = new-object System.Collections.Specialized.StringCollection
PS SQLSERVER:\SQL\WIN7BOOT\SQL1> $sc.Add("C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL1\MSSQL\DATA\hsg.mdf")
PS SQLSERVER:\SQL\WIN7BOOT\SQL1> $sc.Add("C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL1\MSSQL\DATA\hsg_log.ldf)

I detached a database using SQL Server Management Studio and used the following code to attach the database back to the same instance. 我使用SQL Server Management Studio分离了数据库,并使用以下代码将数据库附加到同一实例。 It worked Ok except that the attached database did not get the original name although files were the same. 尽管文件相同,但附加数据库未获得原始名称,效果很好。

PS SQLSERVER:\sql\Hodentek8\RegencyPark\databases> 
$files = new-object system.collections.specialized.stringcollection

$files.add("C:\Program Files\Microsoft SQLServer\MSSQL11.REGENCYPARK\MSSQL\DATA\Feb6.mdf")


$files.add("C:\Program Files\Microsoft SQL Server\MSSQL11.REGENCYPARK\MSSQL\DATA\Feb6_log.ldf")

$server=new-object Microsoft.SqlServer.Management.Smo.Server('Hodentek8\RegencyPark')

$db=new-object Microsoft.SqlServer.Management.Smo.Server

$dbname="feb6"

$server.AttachDatabase($db,$files)

Some details are here: http://hodentekmsss.blogspot.com/2015/04/attaching-detached-database-in-sql.html 一些详细信息在这里: http : //hodentekmsss.blogspot.com/2015/04/attaching-detached-database-in-sql.html

perhaps instead of localhost\\SQLExpress you should use: I found this fix for some of my code which returned the same error: 也许应该使用localhost \\ SQLExpress代替:我为某些返回相同错误的代码找到了此修复程序:

$srv = new-object Microsoft.SqlServer.Management.Smo.Server 'Hodentek8\RegencyPark' 

Replace 'Hodentek8\\RegencyPark' by ("(local)") for the default instance 将“ Hodentek8 \\ RegencyPark”替换为(“(本地)”)作为默认实例

Example here: http://hodentekmsss.blogspot.com/2015/04/counting-sql-server-configuration.html 此处的示例: http : //hodentekmsss.blogspot.com/2015/04/counting-sql-server-configuration.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM