Sql Server SMO 连接超时不起作用

发布于 2024-08-31 05:01:37 字数 1146 浏览 11 评论 0原文

我有以下 PowerShell 代码:

function Get-SmoConnection
{
    param 
        ([string] $serverName = "", [int] $connectionTimeout = 0)

    if($serverName.Length -eq 0)
    {
        $serverConnection = New-Object `
            Microsoft.SqlServer.Management.Common.ServerConnection
    }
    else
    {
        $serverConnection = New-Object `
            Microsoft.SqlServer.Management.Common.ServerConnection($serverName)
    }

    if($connectionTimeout -ne 0)
    {
        $serverConnection.ConnectTimeout = $connectionTimeout
    }

    try
    {
        $serverConnection.Connect()
        $serverConnection
    }
    catch [system.Management.Automation.MethodInvocationException]
    {
        $null
    }


}

$connection = get-smoconnection "ServerName"  2

if($connection -ne $null)
{
    Write-Host $connection.ServerInstance
    Write-Host $connection.ConnectTimeout
}
else
{
    Write-Host "Connection could not be established"
}

它似乎可以工作,除了尝试设置 SMO 连接超时的部分。如果连接成功,我可以验证 ServerConnection.ConnectTimeout 是否设置为 2(秒),但是当我为 SQL Server 实例提供虚假名称时,它仍然尝试连接到它约 15 秒(我相信默认超时值)。

有人有设置 SMO 连接超时的经验吗?先感谢您。

I have the following PowerShell code:

function Get-SmoConnection
{
    param 
        ([string] $serverName = "", [int] $connectionTimeout = 0)

    if($serverName.Length -eq 0)
    {
        $serverConnection = New-Object `
            Microsoft.SqlServer.Management.Common.ServerConnection
    }
    else
    {
        $serverConnection = New-Object `
            Microsoft.SqlServer.Management.Common.ServerConnection($serverName)
    }

    if($connectionTimeout -ne 0)
    {
        $serverConnection.ConnectTimeout = $connectionTimeout
    }

    try
    {
        $serverConnection.Connect()
        $serverConnection
    }
    catch [system.Management.Automation.MethodInvocationException]
    {
        $null
    }


}

$connection = get-smoconnection "ServerName"  2

if($connection -ne $null)
{
    Write-Host $connection.ServerInstance
    Write-Host $connection.ConnectTimeout
}
else
{
    Write-Host "Connection could not be established"
}

It seems to work, except for the part that attempts to set the SMO connection timeout. If the connection is successful, I can verify that ServerConnection.ConnectTimeout is set to 2 (seconds), but when I supply a bogus name for the SQL Server instance, it still attempts to connect to it for ~ 15 seconds (which is I believe the default timeout value).

Does anyone have experience with setting SMO connection timeout? Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装爱人 2024-09-07 05:01:37

我似乎无法重现您所看到的行为。如果将函数重新创建为脚本而不是函数,则无论服务器名称参数是否为假,ConnectionTimeout 属性似乎都可以工作:

Measure-Command {./get-smoconnection.ps1 'Z03\sq2k8' 2}

I can't seem to reproduce the behavior you are seeing. If recreate your function as script rather than a function the ConnectionTimeout property seems to work regardless of whether the server name parameter is bogus or not:

Measure-Command {./get-smoconnection.ps1 'Z03\sq2k8' 2}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文