2016-09-30 81 views
2

在PowerShell中返回多个值使用PowerShell嵌入到应用获得一些数据库值回。 SQL查询可以有多个值。我得到了代码来为我返回一个单一的值。但我似乎没有得到超过一个记录(或我无法访问它)。而且,我也无法使用.Count来查看记录的数量。我如何从SQL查询

此代码适用于一个单一的数据库记录:

  $conn=New-Object System.Data.SqlClient.SQLConnection("Persist Security Info=True;Initial Catalog=;Data Source=;User ID=;Password=;MultipleActiveResultSets=True;") 
      $conn.Open() | out-null 
      $cmd1=$conn.CreateCommand() 
      $cmd1.CommandType = [System.Data.CommandType]::Text 
      $cmd1.CommandText = ("SELECT Supplier_Code FROM V_SUPPLIERINFO WHERE ACN='" + $Context.File.Field[10] + "'") 
      $reader = $cmd1.ExecuteReader() 
      #LOOP THROUGH RECORDS 
      for ($i=0; $i -lt $cmd1.Count; $i++){ 
      } 
      $Context.File.Notes = "I have past the File Note - Count Script: " + $cmd1.Count 
      $reader.Read() | out-null 
      $Context.File.Field[11] = $reader.GetString(0) 
      $Context.File.Save() 
      return "done" 

现在,我想通过所有的结果循环。理想情况下,我使用$ reader.count这样的东西来获得返回结果的数量。但是,.count始终返回1.

我试过以下代码以分别计算: $ conn = New-Object System.Data.SqlClient.SQLConnection(“Persist Security Info = True; Initial Catalog =; Data Source =; User ID =; Password =; MultipleActiveResultSets = True;“) $ conn.Open()|外空

  $SQLRecordCount=$conn.CreateCommand() 
      $SQLRecordCount.CommandType = [System.Data.CommandType]::Text 
      $SQLRecordCount.CommandText = ("SELECT COUNT (Supplier_Code) FROM V_SUPPLIERINFO WHERE ACN='" + $Context.File.Field[10] + "'") 
      $reader = $SQLRecordCount.ExecuteReader() 

      $reader.Read() | out-null 
      $Context.File.Notes = "Total DB Records: " + $reader.GetString(0) 

      $cmd1=$conn.CreateCommand() 
      $cmd1.CommandType = [System.Data.CommandType]::Text 
      $cmd1.CommandText = ("SELECT Supplier_Code FROM V_SUPPLIERINFO WHERE ACN='" + $Context.File.Field[10] + "'") 
      $reader = $cmd1.ExecuteReader() 

      #$Context.File.Notes = "Total DB Records: " + $cmd1.length 

      #LOOP THROUGH RECORDS 
      for ($i=0; $i -lt $cmd1.Count; $i++){ 
      } 

      $reader.Read() | out-null 
      $Context.File.Field[11] = $reader.GetString(0) 
      $Context.File.Save() 
      return "done" 

但总是返回值1

任何想法,我是怎么找到的记录数或如何我可以通过记录循环?

回答

2

我想你应该在$读者寻找,而不是$ CMD1。 This MSDN blog post显示了与您正在尝试执行的操作类似的示例。

从帖子:

# Create and open a database connection 
$sqlConnection = new-object System.Data.SqlClient.SqlConnection “server=SERVERNAME\INSTANCE;database=AdventureWorks;Integrated Security=sspi” 
$sqlConnection.Open() 

#Create a command object 
$sqlCommand = $sqlConnection.CreateCommand() 

$sqlCommand.CommandText = “select FirstName, LastName from Person.Contact where LastName like 'W%'” 

#Execute the Command 
$sqlReader = $sqlCommand.ExecuteReader() 

#Parse the records 
while ($sqlReader.Read()) { $sqlReader[“LastName”] + “, ” + $sqlReader[“FirstName”]} 

# Close the database connection 
$sqlConnection.Close() 
+0

感谢BenH,解析部分与同时做了伎俩。那我也不需要伯爵。 – RogerV

0

变化$读卡器= $ cmd1.ExecuteReader() TO:$读卡器= $ cmd1.ExecuteReader([的System.Data.CommandBehavior] :: CloseConnection)

然后在列值迭代只需拨打

$读者|的foreach对象{$ _。项目(“Supplier_Code”)}

0

你真的做的事情有困难的方式.... 我会建议使用内置的PowerShell Invoke-Sqlcmd或使用模块我开发了名为InvokeQuery ,这更加灵活和强大。无论哪种方式,你会得到一个适当的PowerShell数组,这是微不足道的执行计数和遍历结果。它也少得多的代码行。

#Install-Module -Name InvokeQuery 
$server = "mysqlserver.mydomain.com" 
$db = "mydatabaseName" 
$params = @{ "ACN"=$Context.File.Field[10] } 
$sql = "SELECT Supplier_Code FROM V_SUPPLIERINFO WHERE [email protected];" 
$results = Invoke-SqlServerQuery -Sql $sql -Server $server -Database $db -Parameters $params 
# now getting a count is trivial 
Write-Host "Returned $($results.count) rows." 
# and you can use standard powershell formatting to view the data in table format: 
$results | ft 

另外,如果你只检索从数据库查询一个值,在你的第二个代码块,你并不需要使用阅读器,可以使用ExecuteScalar方法,它会返回第一行第一列的值。我的PowerShell模块有一个类似的开关,以及:

## same code as above. 
$sql = "SELECT count(*) FROM V_SUPPLIERINFO WHERE [email protected];" 
$count = Invoke-SqlServerQuery -Sql $sql -Server $server -Database $db -Parameters $params -Scalar 
Write-Host "The row count is $count."