PoshCode Archive  Artifact [800056bc63]

Artifact 800056bc6302715e3c338a14d3410d7065e9bef3f8fcd256e2adf5cc8e4d06a6:

  • File Invoke-SqlCmd2.ps1 — part of check-in [2b343f47c6] at 2018-06-10 13:01:13 on branch trunk — Implements SQL Server 2008 Invoke-Sqlcmd and addresses bug where querytimeout does not work correctly https://connect.microsoft.com/SQLServer/feedback/details/551799/invoke-sqlcmd-querytimeout-0-still-times-out (user: unknown size: 1045)

# encoding: ascii
# api: powershell
# title: Invoke-SqlCmd2
# description: Implements SQL Server 2008 Invoke-Sqlcmd and addresses bug where querytimeout does not work correctly https://connect.microsoft.com/SQLServer/feedback/details/551799/invoke-sqlcmd-querytimeout-0-still-times-out
# version: 0.1
# type: function
# license: CC0
# function: Invoke-Sqlcmd2
# x-poshcode-id: 1789
# x-archived: 2010-04-24T23:04:31
#
#
function Invoke-Sqlcmd2
{
    param(
    [string]$ServerInstance,
    [string]$Database,
    [string]$Query,
    [Int32]$QueryTimeout=30
    )

    $conn=new-object System.Data.SqlClient.SQLConnection
    $conn.ConnectionString="Server={0};Database={1};Integrated Security=True" -f $ServerInstance,$Database
    $conn.Open()
    $cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn)
    $cmd.CommandTimeout=$QueryTimeout
    $ds=New-Object system.Data.DataSet
    $da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
    [void]$da.fill($ds)
    $ds.Tables[0]
    $conn.Close()
}