在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