Hope my question is in the right queue, it's not very much related with "management".
I'm trying to run some query against my SQL server database. I "stealed" some code from a blog, but I can't understand them.
partial code:
....#defining connection string etc
$Reader = $Command.ExecuteReader()
$Counter = $Reader.FieldCount
while ($Reader.Read()) {
$SQLObject = @{}
for ($i = 0; $i -lt $Counter; $i++) {
$SQLObject.Add(
$Reader.GetName($i),
$Reader.GetValue($i));
}
$SQLObject
}
$Connection.Close()
=======end of code======
My question is focused on $SQLObject = @{}. I guess "@{}" means define an empty hashtable, but I still failed to run my own script without the for loop.
My code:
...#define connection string etc.
$conn.open()
$cmd = $conn.CreateCommand()
$cmd.CommandText = "select avg(boottime) from cdcs.dbo.bootperf"
$Reader=$cmd.ExecuteReader()
$SQLObject = @{}
$SQLObject.Add($Reader.GetName(0),$Reader.GetValue(0));
$conn.close()
====EOF====
error message is "Exception calling "GetValue" with "1" argument(s): "No data exists for the row/column."". And I'm very sure my query will only return one row.
Also, the datatype of $reader is very new to me: SQLSystem.Data.Common.DataRecordInternal.
Any ideas how to deal with $reader?
The blog link is below:
http://www.powershell.nu/2009/01/27...owershell/TIA.