在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
任何想法,我是怎麼找到的記錄數或如何我可以通過記錄循環?
感謝BenH,解析部分與同時做了伎倆。那我也不需要伯爵。 – RogerV