這裏是一個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]
有什麼不對?
這不起作用。發生錯誤: 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 - 同樣的錯誤發生。 –