2016-09-30 76 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."