我成功地使用Powershell和SMO来备份大多数数据库.但是,我有几个大型数据库,其中收到"超时"错误"System.Data.SqlClient.SqlException:Timeout expired".时间一直发生在10分钟.我已经尝试将ConnectionContext.StatementTimeout设置为0,6000,并设置为[System.Int32] :: MaxValue.设置没有任何区别.我发现了许多Google引用,表明将其设置为0会使其无限制.无论我尝试什么,超时都会持续发生在10分钟.我甚至将服务器上的远程查询超时设置为0(通过Studio Manager)无济于事.下面是我的SMO连接,我设置了超时和实际的备份功能.下面是我脚本的输出.
更新 有趣的是,我使用VS 2008在C#中编写了备份函数,并且超时覆盖在该环境中工作.我正在将C#进程合并到我的Powershell脚本中,直到我找到为什么超时覆盖不能仅与Powershell一起使用.这非常烦人!
function New-SMOconnection {
Param ($server,
$ApplicationName= "PowerShell SMO",
[int]$StatementTimeout = 0
)
# Write-Debug "Function: New-SMOconnection $server $connectionname $commandtimeout"
if (test-path variable:\conn) {
$conn.connectioncontext.disconnect()
} else {
$conn = New-Object('Microsoft.SqlServer.Management.Smo.Server') $server
}
$conn.connectioncontext.applicationName = $applicationName
$conn.ConnectionContext.StatementTimeout = $StatementTimeout
$conn.connectioncontext.Connect()
$conn
}
$smo = New-SMOConnection -server $server
if ($smo.connectioncontext.isopen -eq $false) {
Throw "Could not connect to server $($server)."
}
Function Backup-Database {
Param([string]$dbname)
$db = $smo.Databases.get_Item($dbname)
if (!$db) {"Database $dbname was …Run Code Online (Sandbox Code Playgroud)