Powershell运行多个SQL查询

时间:2017-01-26 15:05:20

标签: sql powershell output

我正在尝试运行多个SQL查询并在某些txt / csv文件中获得结果,但是我收到的错误是我无法解决的。有什么建议吗?

$dbServer = "dbserver"
$extractFile = "C:\test\" + $timer + ".csv"
$Database = "b
$User = "Admin"
$Pass = "psswd"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$dbServer;Database=$db;uid=$User;password=$Pass;"
$SQLConnection.Open()
$SQLCommand = $SQLConnection.CreateCommand()
$SQLCommand.CommandText = 
"select count(*) 'Nombre de Address mail' FROM dbo.UsersParticipations"
"select count(*) 'Nombre de Transactions' FROM dbo.Transactions;"
$readAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$readSet = New-Object System.Data.DataSet
$readAdapter.SelectCommand = $SQLCommand
$readAdapter.Fill($readSet) |out-null
$SQLConnection.Close()
    Foreach ($row in $readSet.Tables[0].rows) {
   Write-Output "Adress"
   Write-Output "$($row.size)  $($row.job)"
}
Foreach ($row in $readSet.Tables[1].rows) {
   Write-Output "Transactions"
   Write-Output "$($row.free)  "
}

这是我得到的错误:

Exception calling "Fill" with "1" argument(s): "Invalid object name 'dbo.UsersParticipations'." 
At C:\Efiester\test2.ps1:18 char:1 
+ $readAdapter.Fill($readSet) |out-null 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : SqlException

1 个答案:

答案 0 :(得分:0)

这一位应该是:

...
$SQLCommand.CommandText = 
"select count(*) 'Nombre de Address mail' FROM dbo.UsersParticipations; select count(*) 'Nombre de Transactions' FROM dbo.Transactions"
...

不确定是否会对所有错误进行排序,但它是一个开始