首页 > 代码库 > PowerShell查询sql server
PowerShell查询sql server
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。