c# - Iterating over multiple rows of data in PowerShell -
I am writing some power-shell code to connect to a database, read several lines of data, and return them < Code> SqlDataReader .
I wrote this "C # path" how I would write it: C #:
function ExecuteQuery ($ sql) {$ Connection = New object system.data. SqlClient.SQLConnection ($ ConnectionString); $ Connection.Open (); $ CMD = $ connection.CreateCommand (); $ Cmd.CommandText = $ sql; $ Cmd.Connection = $ connection $ reader = [System.Data.SqlClient.SqlDataReader] $ cmd.ExecuteReader () $ Reader returns; }
(To call this function, we have to close the connection with responsibility.)
I hope, even without typing , Code> System.Data.SqlClient.SqlDataReader . This is important, because I want to use the read
to be repeated from the row through the line, not to mention that I want to use the column name to enter the square-bracket I want to use (Access by ID is very fragile)
Unfortunately, I get an example of the system.Data.Common.DataRecordInternal
, which not was read
The method does not allow me to access the archives by array index.
My question is: How can I read and repeat through several lines of data in PowerShell?
There are ways to repeat through the members of an array ('rows of data' in your case) .
Or you can pipe: this is a forEach loop (you can do pipe output like this, like desired) / Pre>
or if you actually have more granular control If you want to, I think you can use it for loop and do something like this:
$ results = ExecuteQuery $ command string ($ i = 0; $ I -le $ Result. Ct; $ i ++) {$ $ [$ i]}
Comments
Post a Comment