2013-11-25 149 views
0

這裏是一個PowerShell腳本:SMO.Transfer:不能轉換型「數據庫」中鍵入「數據庫」

[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

我還tryed設置$分貝如下:

$db = $srv.Databases[$database] 

有什麼不對?

回答

1

嘗試以下操作:

$db = $srv.Databases[$database] 
... 
$ObjTransfer = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfer -ArgumentList $db 

這應該創建一個使用$db作爲源數據庫傳輸對象。

MSDN: Transferring Schema and Data from One Database to Another in PowerShell

+0

這不起作用。發生錯誤: New-Object:找不到「Transfer」的重載和參數計數:「1」。 在D:\ FT \ FTProject \ Setup \ transfer.ps1:25 char:18 + $ ObjTransfer = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfe ... + ~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo:InvalidOperation:(:) [New-Object],MethodException + FullyQualifiedErrorId:ConstructorInvokedThrowException,Microsoft.PowerShell.Commands 。 我也嘗試了從你的鏈接expamp - 同樣的錯誤發生。 –

0

見例子,我認爲這是因爲Microsoft.SqlServer.Smo.dll和Microsoft.SqlServer.SmoExtended.dll的不同版本進行加載。

我得到當我加載第一組件在10版,另一個在12

當我在同一個版本都明確地加載他們兩個都工作良好,同樣的錯誤。

Add-Type -path "C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Smo\12.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Smo.dll" 

Add-Type -path "C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.SmoExtended\12.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.SmoExtended.dll" 
+0

感謝您的回答!不幸的是我無法測試這個解決方案)) –