Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - System.Data.Common.DbDataReader and change in type

Reply
 
Old 03-31-2008   #1 (permalink)
Jakob Bindslet


 
 

System.Data.Common.DbDataReader and change in type

Hi everyone,

I'm truying to create a small function to execute a query against an
SQL server. After which I try using the function ($conn is a working,
already initialized):

However, the type of $data isn't [System.Data.Common.DbDataReader]
anymore, it has become [System.Array]

Is there any way to avoid this change in type, and why does it occur?



function Query-SQL {
Param ($query, $conn)
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandText = $query
$sqlCmd.Connection = $conn
$Rset = $sqlCmd.ExecuteReader()
$Rset.gettype() # <-- [System.Array]
Return $Rset
}

$data = Query-SQL "SELECT * from master..sysdatabases" $conn

$data.gettype() # <--[System.Data.Common.DbDataReader]


Regards,
Jakob Bindslet, Denmark

My System SpecsSystem Spec
Old 04-01-2008   #2 (permalink)
Oisin (x0n) Grehan [MVP]


 
 

Re: System.Data.Common.DbDataReader and change in type

On Mar 31, 2:34*am, Jakob Bindslet <ja...@xxxxxx> wrote:
Quote:

> Hi everyone,
>
> I'm truying to create a small function to execute a query against an
> SQL server. After which I try using the function ($conn is a working,
> already initialized):
>
> However, the type of $data isn't [System.Data.Common.DbDataReader]
> anymore, it has become [System.Array]
>
> Is there any way to avoid this change in type, and why does it occur?
>
> function Query-SQL {
> * * * * Param ($query, $conn)
> * * * * $sqlCmd = New-Object System.Data.SqlClient.SqlCommand
> * * * * $sqlCmd.CommandText = $query
> * * * * $sqlCmd.Connection = $conn
> * * * * $Rset = $sqlCmd.ExecuteReader()
> * * * * $Rset.gettype() *# <-- [System.Array]
> * * * * Return $Rset
>
> }
>
> $data = Query-SQL "SELECT * from master..sysdatabases" $conn
>
> $data.gettype() # <--[System.Data.Common.DbDataReader]
>
> Regards,
> Jakob Bindslet, Denmark
Hi Jakob,

Sorry noone got to you earlier than this, I just noticed your post.

You're experiencing "powershell shock" ;-) defined by the sudden
realisation that any collections, lists (or indeed data readers) that
implement IEnumerable are being automatically unravelled by
PowerShell. Example:

# define an array of int, with 3 elements
PS> [int[]]$arr = @(1,2,3)

# Now, count objects passed to measure-object

PS> $arr | measure-object

Count : 3
Average :
Sum :
Maximum :
Minimum :
Property :

As you can see, meausre-object detected three elements passed in. You
ask how can this behaviour be curbed? Well, the easiest way is to wrap
the array in another array - PowerShell will strip the outer array
leaving the inner array (contained in the first element) alone.
Easiest way to do this is to you the comma ',' which is the array
constructor operator:

PS> ,$arr | measure-object

Count : 1
<snip>

So, to answer your original question, instead of:
Quote:

> Return $Rset
use:
Quote:

> ,$Rset
You'll notice that I dropped the "return" statement. In powershell
this is seldom needed, and omitting it serves as a good reminder of
the functional nature of PowerShell's script.

Hope this helps,

- Oisin

PowerShell MVP
http://www.nivot.org/
My System SpecsSystem Spec
Old 04-02-2008   #3 (permalink)
Jakob Bindslet


 
 

Re: System.Data.Common.DbDataReader and change in type

On Apr 2, 3:04 am, "Oisin (x0n) Grehan [MVP]" <ois...@xxxxxx>
wrote:
Quote:

> On Mar 31, 2:34 am, Jakob Bindslet <ja...@xxxxxx> wrote:
Quote:

> > Hi everyone,
>
Quote:

> > I'm truying to create a small function to execute a query against an
> > SQL server. After which I try using the function ($conn is a working,
> > already initialized):
>
Quote:

> > However, the type of $data isn't [System.Data.Common.DbDataReader]
> > anymore, it has become [System.Array]
>
Quote:

> > Is there any way to avoid this change in type, and why does it occur?
>
Quote:

> > function Query-SQL {
> > Param ($query, $conn)
> > $sqlCmd = New-Object System.Data.SqlClient.SqlCommand
> > $sqlCmd.CommandText = $query
> > $sqlCmd.Connection = $conn
> > $Rset = $sqlCmd.ExecuteReader()
> > $Rset.gettype() # <-- [System.Array]
> > Return $Rset
>
Quote:

> > }
>
Quote:

> > $data = Query-SQL "SELECT * from master..sysdatabases" $conn
>
Quote:

> > $data.gettype() # <--[System.Data.Common.DbDataReader]
>
Quote:

> > Regards,
> > Jakob Bindslet, Denmark
>
> Hi Jakob,
>
> Sorry noone got to you earlier than this, I just noticed your post.
>
> You're experiencing "powershell shock" ;-) defined by the sudden
> realisation that any collections, lists (or indeed data readers) that
> implement IEnumerable are being automatically unravelled by
> PowerShell. Example:
>
> # define an array of int, with 3 elements
> PS> [int[]]$arr = @(1,2,3)
>
> # Now, count objects passed to measure-object
>
> PS> $arr | measure-object
>
> Count : 3
> Average :
> Sum :
> Maximum :
> Minimum :
> Property :
>
> As you can see, meausre-object detected three elements passed in. You
> ask how can this behaviour be curbed? Well, the easiest way is to wrap
> the array in another array - PowerShell will strip the outer array
> leaving the inner array (contained in the first element) alone.
> Easiest way to do this is to you the comma ',' which is the array
> constructor operator:
>
> PS> ,$arr | measure-object
>
> Count : 1
> <snip>
>
> So, to answer your original question, instead of:
>
Quote:

> > Return $Rset
>
> use:
>
Quote:

> > ,$Rset
>
> You'll notice that I dropped the "return" statement. In powershell
> this is seldom needed, and omitting it serves as a good reminder of
> the functional nature of PowerShell's script.
>
> Hope this helps,
>
> - Oisin
>
> PowerShell MVPhttp://www.nivot.org/
Thanks for the answer. It appears to have solved my problem!



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Sharing common data properties between multiple users in Vista System Security
Re: Unable to cast COM object of type 'ADODB.RecordsetClass' to class type 'System.Object[]' .NET General
Dual boot XP/Vista sharing common installed applications and data? Vista installation & setup
Common App Data Vista General
common pipeline problem i have. (need data from an earler stage, that has been lost) PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46