【问题标题】:Powershell run multiple SQL queriesPowershell 运行多个 SQL 查询
【发布时间】:2017-01-26 15:05:20
【问题描述】:

我正在尝试运行多个 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”参数调用“Fill”的异常:“无效的对象名称'dbo.UsersParticipations'。”在 C:\Efiester\test2.ps1:18 char:1 + $readAdapter.Fill($readSet) |out-null + ~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SqlException
  • 从评论中添加了错误信息

标签: sql powershell output


【解决方案1】:

这个位应该是:

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

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

【讨论】:

  • 您确定表 UsersParticipations 存在并且在模式 dbo 下吗?你试过直接在sql server上查询吗?
猜你喜欢
  • 2013-08-10
  • 1970-01-01
  • 2017-09-21
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多