Register
: :
Login
Home
Forums
Blogs
Podcast
Directories
Scripts
Downloads
Community
User Group Support
Learning Resources
We have a new sponsor! Introducting
Pragma Systems
. See the home page for details.
Unanswered
Active Topics
Forums
Search
Members
Forums
>
Using PowerShell
>
SQL Server
Question about run query against
Last Post 16 Oct 2009 12:42 PM by
rrrrr5r
. 3 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
rrrrr5r
New Member
Posts:4
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.
seaJhawk
Basic Member
Posts:190
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$]'
rrrrr5r
New Member
Posts:4
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.
rrrrr5r
New Member
Posts:4
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.
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
Forums
>
Using PowerShell
>
SQL Server
Active Forums 4.1
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008