【问题标题】:powershell System.Collections.Generic.List[System.String] and foreachpowershell System.Collections.Generic.List[System.String] 和 foreach
【发布时间】:2018-12-21 20:35:40
【问题描述】:

我发现我有以下通用列表,并且我可以看到其中有项目,但是当我尝试运行代码时,它没有在 foreach 中命中。这是我的代码:

function SQLQueryWriteToFile([string]$SQLquery, [string]$extractFile)
{

   $sqlConnection = New-Object System.Data.SqlClient.SqlConnection

   $sqlConnection.ConnectionString = "Server=blah;Database=blah;User ID=blah;Password=blah"  #production #I have an error in this so it doesn't connect
      $sqlConnection.Open()
   if($sqlConnection.State -ne 'Open'){
      $global:ErrorStrings.Add("Exception: $("Couldn't connect to DB with connection string given");;  ") #this gets hit
   }

###

$global:ErrorStrings = New-Object System.Collections.Generic.List[System.String] #System.Object]
$query = "Select blah"
$dir = "C:\blah"

SQLQueryWriteToFile $query $dir

$errorCodeAsString = ""

foreach ($item in $global:ErrorStrings.Members){
   $errorCodeAsString += $item  #this isn't hit
}

知道为什么它没有在我的 foreach 循环列表中找到错误字符串,当我看到它在那里查看 $global:ErrorStrings 时?基于这个foreach list,我做对了。我很难找到像我正在做的例子。谢谢!

【问题讨论】:

  • 如图所示,您创建了列表,但从不向其中添加任何内容。
  • 在此行打开数据库连接失败时添加到列表中:$global:ErrorStrings.Add("Exception: $("Couldn't connect to DB with connection string
  • 我找不到 generic.list 的 .Members 属性...$global:ErrorStrings.Members 来自哪里?
  • 我是从我的链接的例子中得到的。
  • @Michele - 很高兴听到你修复了它! [grin] ///// 下次出现问题时...尝试查看所涉及的 $Var 的输出。在这种情况下尝试使用 .Members 时,您会收到错误或空白。这是在解释设置中工作的真正妙处之一……您可以交互式地探索这些项目! [咧嘴]

标签: list powershell foreach


【解决方案1】:

试试这个:

function SQLQueryWriteToFile([string]$SQLquery, [string]$extractFile)
{
  [System.Data.SqlClient.SqlConnection] $sqlConnection=$null
  [System.Data.SqlClient.SqlCommand] $command=$null 
  try
  {
      $sqlConnection = New-Object System.Data.SqlClient.SqlConnection
      $sqlConnection.ConnectionString = "Server=blah;Database=blah;User ID=blah;Password=blah"
      $command = New-Object System.Data.SqlClient.SqlCommand
      $command.Connection=$sqlConnection
      $command.CommandText=$SQLquery

      $sqlConnection.Open()
      $command.ExecuteNonQuery()

  }
  catch
  {
      $global:ErrorStrings.Add($_.Exception.Message)
  }
  finally
  {
      if ($sqlConnection -ne $null) 
      {
        $sqlConnection.Close()
        $sqlConnection.Dispose()
      }

      if ($command -ne $null) 
      {
        $command.Dispose()
      }
  }

}


$global:ErrorStrings = New-Object System.Collections.Generic.List[System.String]
$query = "Select blah"
$dir = "C:\blah"

$global:ErrorStrings.Clear()
SQLQueryWriteToFile $query $dir

$errorCodeAsString=""

for ($i = 0; $i -lt $global:ErrorStrings.Count; $i++)
{ 
    $errorCodeAsString +=$global:ErrorStrings.Item($i)
}

$errorCodeAsString

【讨论】:

  • 非常感谢 Esperento57!我无法执行NonQuery,因为它不是更新等,但它捕获了错误登录的异常。此外,for 循环也有效。谢谢!
  • 为你高兴 :) 是的,我使用了 executenonquery,但你可以像往常一样使用 executequery 和数据集,这是为了完成示例。祝你有美好的一天
猜你喜欢
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 2014-03-14
  • 2018-03-09
  • 1970-01-01
相关资源
最近更新 更多