Chr*_*son 152
对于其他只需要库存.net和PowerShell(没有安装其他SQL工具)的人来说,这里是我使用的功能:
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
}
Run Code Online (Sandbox Code Playgroud)
我一直在使用这么长时间我不知道是谁编写了哪些部分,但这是从其他的例子中提炼出来的,但是简化为清楚,只需要没有额外的依赖关系或功能.
我经常使用和分享这个,我把它变成了GitHub上的一个脚本模块,这样你现在可以进入你的模块目录并执行git clone https://github.com/ChrisMagnuson/InvokeSQL,从那时起,当你去使用它时,将自动加载invoke-sql(假设你使用的是powershell v3或更高版本).
man*_*lds 98
您可以使用Invoke-Sqlcmdcmdlet
Invoke-Sqlcmd -Query "SELECT GETDATE() AS TimeOfQuery;" -ServerInstance "MyComputer\MyInstance"
Run Code Online (Sandbox Code Playgroud)
http://technet.microsoft.com/en-us/library/cc281720.aspx
dmc*_*dmc 27
这是我在这个博客上找到的一个例子.
$cn2 = new-object system.data.SqlClient.SQLConnection("Data Source=machine1;Integrated Security=SSPI;Initial Catalog=master");
$cmd = new-object system.data.sqlclient.sqlcommand("dbcc freeproccache", $cn2);
$cn2.Open();
if ($cmd.ExecuteNonQuery() -ne -1)
{
echo "Failed";
}
$cn2.Close();
Run Code Online (Sandbox Code Playgroud)
据推测,您可以用它所代替的不同TSQL语句替换它dbcc freeproccache.
mco*_*ien 24
此函数将作为powershell对象数组返回查询结果,以便您可以轻松地在过滤器和访问列中使用它们:
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
}
Run Code Online (Sandbox Code Playgroud)
arn*_*nav 11
Invoke-Sqlcmd -Query "sp_who" -ServerInstance . -QueryTimeout 3
Run Code Online (Sandbox Code Playgroud)
没有内置的"PowerShell"方式来运行SQL查询.如果安装了SQL Server工具,则会获得Invoke-SqlCmd cmdlet.
由于PowerShell是基于.NET构建的,因此您可以使用ADO.NET API来运行查询.
小智 8
如果您想在本地计算机上而不是在SQL服务器的上下文中执行此操作,那么我将使用以下内容.这是我们在公司使用的.
$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
Run Code Online (Sandbox Code Playgroud)
只需填写$ ServerName,$ DatabaseName和$ Query变量,你应该好好去.
我不确定我们最初是如何发现这一点的,但这里有一些非常类似的东西.
| 归档时间: |
|
| 查看次数: |
292651 次 |
| 最近记录: |