[英]Powershell: Exception calling “AttachDatabase” with “3” argument(s)
I 'm trying to backup the mdf file using the following approach but the script fails when attaching the database back to the server. 我正在尝试使用以下方法备份mdf文件,但是将数据库附加到服务器时脚本失败。 I have started facing this issue in our new server (Windows Server 2008 R2).
我已经开始在新服务器(Windows Server 2008 R2)中解决此问题。 The following is the scriplet.
以下是片段。
#load assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
write-host "Initializing..."
#Initialization section
$serverName = "sqlserver\SQL2005"
$databaseName = "myDatabase"
$sourceLocation = "\\sqlserver\dbPath\myDatabase"
$attachDBSourceLocation = "D:\Sql 2005 Databases\dbPath\myDatabase"
$mdfFileName = $sourceLocation + "\" + $databaseName + ".mdf"
$attachMDFFileName = $attachDBSourceLocation + "\" + $databaseName + ".mdf"
$ldfFileName = $sourceLocation + "\" + $databaseName + "_log.ldf"
$destLocation = read-host -prompt "Enter the destination location"
#End of Initialization section
write-host "Initialization completed"
#create a new server object
$serverConn = New-Object ("Microsoft.SqlServer.Management.Common.ServerConnection")
$serverName, "psuser", "psuser"
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $serverConn
write-host "Server object created"
$server.Databases["myDatabase"].ExecuteNonQuery("ALTER DATABASE myDatabase SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE", [Microsoft.SqlServer.Management.Common.ExecutionTypes]::Default)
#detach the database to be copied. Drop connections if any by changing the connection mode to single user mode
$server.DetachDatabase($databaseName, $true)
write-host "Database $databaseName detached"
#copy the database (backup)
write-host "Started copying database '$mdfFileName' to '$destLocation'. Please wait..."
copy-item $mdfFileName -Destination $destLocation
write-host "Database '$databaseName' copied to '$destLocation'"
#delete the log file
remove-item -path $ldfFileName
write-host "Database LDF file deleted" #attach the mdf file without ldf so that a new ldf is automatically created
write-host "Attaching Database '$databaseName'. Please wait..."
$strColl = New-Object ("System.Collections.Specialized.StringCollection")
[void]$strColl.Add($attachMDFFileName)
$server.AttachDatabase($databaseName, $strColl, [Microsoft.SqlServer.Management.Smo.AttachOptions]::NewBroker)
write-host "Database attached successfully"
write-host "Shrinking Database '$databaseName'. Please wait..."
$server.Databases[$databaseName].ExecuteNonQuery("DBCC SHRINKDATABASE ($databaseName, 5)")
write-host "Database shrinked successfully"
write-host "Database backup successful"
Are you sure you need that attachmentoptions overload? 您确定您需要附件选项重载吗? From the overload list for that method:
从该方法的重载列表中:
http://msdn.microsoft.com/en-US/library/microsoft.sqlserver.management.smo.server.attachdatabase(v=SQL.90).aspx http://msdn.microsoft.com/zh-CN/library/microsoft.sqlserver.management.smo.server.attachdatabase(v=SQL.90).aspx
it doesn't appear that you need to specify that if you're re-attaching an existing database, just the database name and the file collection. 似乎不需要指定是否要重新连接现有数据库,只需指定数据库名称和文件集合即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.