PowerShell查詢sql server

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   strong   ar   for   

function Invoke-SQL1:

function Invoke-SQL {    param(        [string] $dataSource = ".\SQLEXPRESS",        [string] $database = "MasterData",        [string] $sqlCommand = $(throw "Please specify a query.")      )    $connectionString = "Data Source=$dataSource; " +            "Integrated Security=SSPI; " +            "Initial Catalog=$database"    $connection = new-object system.data.SqlClient.SQLConnection($connectionString)    $command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)    $connection.Open()    $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command    $dataset = New-Object System.Data.DataSet    $adapter.Fill($dataSet) | Out-Null    $connection.Close()    $dataSet.Tables}

function SQL2:

function sql($sqlText, $database = "master", $server = "."){    $connection = new-object System.Data.SqlClient.SQLConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database");    $cmd = new-object System.Data.SqlClient.SqlCommand($sqlText, $connection);    $connection.Open();    $reader = $cmd.ExecuteReader()    $results = @()    while ($reader.Read())    {        $row = @{}        for ($i = 0; $i -lt $reader.FieldCount; $i++)        {            $row[$reader.GetName($i)] = $reader.GetValue($i)        }        $results += new-object psobject -property $row                }    $connection.Close();    $results}

SQL Query3:

$ServerName = "_ServerName_"$DatabaseName = "_DatabaseName_"$Query = "SELECT * FROM Table WHERE Column = ‘‘"#Timeout parameters$QueryTimeout = 120$ConnectionTimeout = 30#Action of connecting to the Database and executing the query and returning results if there were any.$conn=New-Object System.Data.SqlClient.SQLConnection$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerName,$DatabaseName,$ConnectionTimeout$conn.ConnectionString=$ConnectionString$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

 

from:http://stackoverflow.com/questions/8423541/how-do-you-run-a-sql-server-query-from-powershell

PowerShell查詢sql server

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.