这是一个powershell脚本:
[string] $server = "devserver\mssql"; # SQL Server Instance
[string] $database = "ftg"; # Database with the tables to script out.
[string] $folder = "d:\FT\FTProject\"; # Path to export to
[string] $SQLLogin = "Sa";
[string] $SQLPass = "Sa1234";
# Reference to SMO
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO');
Write-Output ((Get-Date -format yyyy-MM-dd_HH-mm-ss) + ": Started ...");
$con = New-Object Microsoft.SqlServer.Management.Common.ServerConnection($server, $SQLLogin, $SQLPass);
$srv = New-Object Microsoft.SqlServer.Management.SMO.Server($con);
$db = New-Object Microsoft.SqlServer.Management.SMO.Database($srv, $database);
#Use SMO Transfer Class by specifying source database
#you can specify properties you want either brought over or excluded, when the copy happens
$ObjTransfer = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfer;
$ObjTransfer.Database = $db
在最后一行发生错误:
异常设置“数据库”:“无法将”Microsoft.SqlServer.Management.Smo.Database“类型的”[ftg]“值转换为”Microsoft.SqlServer.Managem“ ent.Smo.Database “” 在D:\ FT \ FTProject \ Setup \ transfer.ps1:31 char:1 + $ ObjTransfer.Database = [Microsoft.SqlServer.Management.SMO.Database] $ db + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],SetValueInvocationException + FullyQualifiedErrorId:ExceptionWhenSetting
我还尝试按如下方式设置$ db:
$db = $srv.Databases[$database]
有什么问题?
答案 0 :(得分:1)
尝试以下方法:
$db = $srv.Databases[$database]
...
$ObjTransfer = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfer -ArgumentList $db
这应该使用$db
作为源数据库创建Transfer对象。
请参阅MSDN: Transferring Schema and Data from One Database to Another in PowerShell
上的示例答案 1 :(得分:0)
我认为是因为加载了不同版本的Microsoft.SqlServer.Smo.dll和Microsoft.SqlServer.SmoExtended.dll。
当我在10个版本中加载第一个程序集而在12个程序中加载另一个程序集时,我得到了同样的错误。
当我在同一版本中明确加载它们时,一切正常。
{{1}}