header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Question about run query against
Last Post 16 Oct 2009 04:42 AM by Sean. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SeanUser is Offline
New Member
New Member
Posts:4
Avatar

--
15 Oct 2009 05:45 AM
    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.


    seaJhawkUser is Offline
    Basic Member
    Basic Member
    Posts:191
    Avatar

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

    http://www.leeholmes.com/blog/Categ...20c1033469

    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$]'

    SeanUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

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

    --
    16 Oct 2009 04:42 AM
    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.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer