-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-SQL.ps1
More file actions
29 lines (22 loc) · 882 Bytes
/
Invoke-SQL.ps1
File metadata and controls
29 lines (22 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function Invoke-SQL {
param(
[string] $DataSource = ".\SQLEXPRESS",
[string] $Database = "MasterData",
[string] $Query = $(throw "Please specify a query."),
[PSCredential] $Credential = $null
)
$connectionString = "Data Source=$DataSource; " +
"Integrated Security=SSPI; " +
"Initial Catalog=$Database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
if ($Credential -ne $null) {
$connection.Credential = $Credential
}
$command = new-object system.data.sqlclient.sqlcommand($Query,$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
}