header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

Question about run query against
Last Post 16 Oct 2009 12:42 PM by rrrrr5r. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
rrrrr5rUser is Offline
New Member
New Member
Posts:4
Avatar

--
15 Oct 2009 01:45 PM  
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/handy-sql-function-in-powershell/

TIA.


seaJhawkUser is Offline
Basic Member
Basic Member
Posts:190
Avatar

--
15 Oct 2009 07:35 PM  
My suggestion is run, don't walk, to Lee Holmes' script: Invoke-SQLCommand.ps1.

http://www.leeholmes.com/blog/CategoryView,category,guide.aspx#a07815475-fbf5-4178-bb6e-fb20c1033469

Unless that is you have SQL 2008. Then you can use the cmdlets with SQL 2008, but they may not do cool things like these that Lee's script does:

## ## Use Windows authentication
## Invoke-SqlCommand.ps1 -Sql "SELECT TOP 10 * FROM Orders"
##
## ## Use SQL Authentication
## $cred = Get-Credential
## Invoke-SqlCommand.ps1 -Sql "SELECT TOP 10 * FROM Orders" -Cred $cred
##
## ## Perform an update
## $server = "MYSERVER"
## $database = "Master"
## $sql = "UPDATE Orders SET EmployeeID = 6 WHERE OrderID = 10248"
## Invoke-SqlCommand $server $database $sql
##
## $sql = "EXEC SalesByCategory 'Beverages'"
## Invoke-SqlCommand -Sql $sql
##
## ## Access an access database
## Invoke-SqlCommand (Resolve-Path access_test.mdb) -Sql "SELECT * FROM Users"
##
## ## Access an excel file
## Invoke-SqlCommand (Resolve-Path xls_test.xls) -Sql 'SELECT * FROM [Sheet1$]'

rrrrr5rUser is Offline
New Member
New Member
Posts:4
Avatar

--
16 Oct 2009 12:42 PM  
I wish I can. I have to leave it along for now, but I have to figure it out sometime.
rrrrr5rUser is Offline
New Member
New Member
Posts:4
Avatar

--
16 Oct 2009 12:42 PM  
I wish I can. I have to leave it along for now, but I have to figure it out sometime.

thank you for reply.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer