【问题标题】:Run write-host output while SqlConnection.Open() is running在 SqlConnection.Open() 运行时运行 write-host 输出
【发布时间】:2017-09-25 20:14:27
【问题描述】:

我希望有一个尾随.....在我运行SqlConnection.Open()时扩展/增长。

我不知道如何执行此操作,并且当我运行命令 SqlConnection.Open() 时输出停止,直到建立连接然后我的代码继续。

我尝试了一个 while 循环,但 while 内容在实际连接建立或失败之前什么都不做,这可能需要 10 -15 秒。

$waiting = ".", ".", ".", ".", ".";
Try
{
    #write-host -ForegroundColor GREEN "Connecting to SQL Server: $svr"
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server = $svr ;Database = $db; User ID = $uid ;Password = $pwd;"

    while (!$SqlConnection.State -eq 'Open')
    {
        write-host -ForegroundColor GREEN "Connecting to SQL Server: $svr" -NoNewline
        ForEach ($p in $waiting) {

            Write-Host  -ForegroundColor Cyan "`r$p" -NoNewLine
            Start-Sleep -Milliseconds 300
        }

        $SqlConnection.Open()  
    }

    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.Connection = $Global:SqlConnection
    $SqlCmd.CommandText = $UsrSqlQuery
}

【问题讨论】:

  • 你能告诉我们你到目前为止的相关代码吗?
  • 这就是我目前的情况,已经有一些迭代 do while 和其他 while 循环

标签: sql-server powershell while-loop sqlconnection parallel.foreach


【解决方案1】:

您有一个单线程脚本。一旦你调用了 .Open 方法,在它返回之前什么都不会发生。我认为多线程是你得到你想要的东西的唯一方法。 这是一个PS中的多线程示例: multithreading

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 2022-01-22
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 2020-03-31
    • 2020-02-03
    相关资源
    最近更新 更多