Search
 
SCRIPT & CODE EXAMPLE
 

CPP

powershell script query mssql windows authentication

function global:SelectAllUsers()
{
    Read-Query -ConnectionString 'Server=localhost;Database=Ulysses;UID=EMEAXJ193;PWD=somepassword;Integrated Security=true;' `
        -Query "SELECT * FROM Users" `
        -Action {
            echo "I can take an action here"
        }
}

function Read-Query
{
    param (
        [Parameter(Mandatory=$true)]
        [string]$ConnectionString,

        [Parameter(Mandatory=$true)]
        [string]$Query,

        [Parameter(Mandatory=$true)]
        [scriptblock]$Action
    )

    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = $ConnectionString
    $SqlConnection.Open()
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $Query
    $SqlCmd.Connection = $SqlConnection
    $reader = $SqlCmd.ExecuteReader()

    while ($reader.Read())
    {
        $x = $null
        $x = @{}

        for ($i = 0; $i -lt $reader.FieldCount; ++$i)
        {
            $x.add($reader.GetName($i), $reader[$i])
        }

        Invoke-Command -ScriptBlock $action -ArgumentList $x
    }

    $SqlConnection.Close()
}



SelectAllUsers
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ click event 
Cpp :: como copiar codigo de c++ con numeros de fila en docs 
Cpp :: c++ int max value 
Cpp :: c++ map change order 
Cpp :: a suprise... c++ 
Cpp :: how initilaize deffult value to c++ class 
Cpp :: get shape of eigen matrix 
Cpp :: map::begin 
Cpp :: Corong_ExerciseNo3(1) 
Cpp :: servicenow cart api 
Cpp :: statement that causes a function to end in c++ 
Cpp :: static_cast 
Cpp :: ue4 c++ string from fvector 
Cpp :: Diamond pattren program in C++ 
Cpp :: fast scan in c++ 
Cpp :: find number of 1s in a binary cv::mat image 
Cpp :: how to replace a element in a vector c++ using index 
Cpp :: template design pattern 
Cpp :: c+ - Dormir en millisecondes 
Cpp :: store binary data in buffer 
Cpp :: lnk2001 unresolved external symbol __imp_PlaySoundA 
Cpp :: why do men drink liquor 
Cpp :: niet werkend 
Cpp :: C++ Ranged Based for Loop 
Cpp :: C++ Point to Every Array Elements 
Cpp :: permutation and combination program in c++ 
Cpp :: dijkstra algorithm in c++ geeksforgeeks 
Cpp :: compile c++ MPI Program 
Cpp :: c++ cout 
Cpp :: C++ area & circumference of a circle 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =