我在应用程序中使用Powershell来获取一些数据库值。 SQL Query可以有多个值。我得到的代码可以让我返回一个值。但我似乎没有回复多条记录(或者我无法访问它)。而且,我也无法使用.Count来查看记录数。
此代码适用于单个数据库记录:
$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。
知道我如何获得记录的数量或如何循环记录?
答案 0 :(得分:2)
我认为你应该关注$ reader而不是$ 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()
答案 1 :(得分:0)
更改$ reader = $ cmd1.ExecuteReader() TO:$ reader = $ cmd1.ExecuteReader([System.Data.CommandBehavior] :: CloseConnection)
然后迭代列值只需调用
$读者| ForEach-Object {$ _。Item(' Supplier_Code')}
答案 2 :(得分: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 ACN=@ACN;"
$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 ACN=@ACN;"
$count = Invoke-SqlServerQuery -Sql $sql -Server $server -Database $db -Parameters $params -Scalar
Write-Host "The row count is $count."