Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long running query when using Invoke-SQL needs timeout setting #45

Open
PeterBizz opened this issue Dec 14, 2017 · 0 comments
Open

Long running query when using Invoke-SQL needs timeout setting #45

PeterBizz opened this issue Dec 14, 2017 · 0 comments

Comments

@PeterBizz
Copy link

Hi when using Invoke-SQL to restore a snapshot I encountered a time-out.
This left the database in restoring state.
Would be nice to have a timeout parameter added to Invoke-SQL, i.e. like this:
Invoke-SQL_SLOW -DatabaseServer $DatabaseServer -DatabaseName 'master' -SQLCommand $RestoreSnapshotSQL -Verbose -ErrorAction Stop -TimeOut 10000

Altered version of Invoke SQL:

function Invoke-SQL_SLOW {
    [CmdLetBinding()]
    param(
        [string] $DatabaseServer = [net.dns]::gethostname(),
        [String] $DatabaseInstance = '',
        [string] $DatabaseName = 'Master',
        [string] $SQLCommand = $(throw 'Please specify a query.'),
        [string] $TimeOut = 30
      )

    if (!([string]::IsNullOrEmpty($DatabaseInstance))){
        $DatabaseServer = "$($DatabaseServer)\$($DatabaseInstance)"
    }
    $connectionString = "Data Source=$DatabaseServer; Integrated Security=SSPI; Initial Catalog=$DatabaseName"

    write-Host -ForegroundColor Green "Invoke-SQL-SLOW with this statement on database '$DatabaseName':"
    Write-Host -ForegroundColor Gray $SQLCommand

    $connection = new-object system.data.SqlClient.SQLConnection($connectionString)
    

    $command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
    $command.CommandTimeout =$TimeOut
    $connection.Open()

    $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
    $dataset = New-Object System.Data.DataSet
    
    $adapter.Fill($dataSet) | Out-Null

    $connection.Close()
    $connection.Dispose()

    $dataSet.Tables

    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants