PoshCode Archive  Artifact [5f5eea951b]

Artifact 5f5eea951b360c9e1a18ed72276bba24852555480e6e539c068093b3e877fc56:

  • File Invoke-SqlCmd2.ps1 — part of check-in [c07af1680f] at 2018-06-10 13:01:16 on branch trunk — Modeled after SQL Server 2008 Invoke-Sqlcmd, but fixes bug in QueryTimeout. Fixed minor issue closing connection. (user: Chad Miller size: 1036)

# encoding: ascii
# api: powershell
# title: Invoke-SqlCmd2
# description: Modeled after SQL Server 2008 Invoke-Sqlcmd, but fixes bug in QueryTimeout. Fixed minor issue closing connection.
# version: 0.1
# type: function
# author: Chad Miller
# license: CC0
# function: Invoke-Sqlcmd2
# x-poshcode-id: 1791
# x-derived-from-id: 2111
# x-archived: 2014-08-29T23:26:54
# x-published: 2010-04-16T14:56:00
#
#
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)
    $conn.Close()
    $ds.Tables[0]

}