[英].Net SQL Server queries from PowerShell
So I have this connection所以我有这个联系
$connectionString = "Data Source=Something ;Initial Catalog=Something ;Integrated Security=SSPI"
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
$query = "select MAX(ID) from [dbo].[Table];"
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $connection)
$connection.Open()
$NewID = $command.ExecuteScalar()
$connection.Close()
echo $NewID
And it works fine and does the job.它工作正常并且可以完成工作。 Say I want to
INSERT A ROW
.说我想
INSERT A ROW
。 Do I need the last 3 lines possibly with another method in $command.method()
?我是否需要最后 3 行可能与
$command.method()
中的另一种方法? In which line is the query actually executed?查询实际执行在哪一行?
Question 2: What method should I use if I want to get the whole result set to PowerShell?问题2:如果想得到整个结果集为PowerShell,应该用什么方法?
###...your code up to $NewID = ...
$rdr = $command.ExecuteReader(1)
#get-type $rdr
$dt = new-object system.data.datatable
$dt.load($rdr)
$dt | out-gridview
If you want to execute a query without a result (like an INSERT
) you can use ExecuteNonQuery如果您想执行没有结果的查询(如
INSERT
),您可以使用ExecuteNonQuery
$command.ExecuteNonQuery()
If you want to to return non-scalar results, use ExecuteReader .如果要返回非标量结果,请使用ExecuteReader 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.