.net - Get name of column from PowerShell sql query -


i'm running sql query powershell. can row data , number of columns correctly, need gather name of columns sql table generated in query. example, if table looked this:

    column1    column2    1 |row1item1 |row1item2 | 2 |row2item1 |row2item2 | 3 |row3item1 |row3item2 | 

then values need "column1", "column2". code have below tell me there 2 columns, don't know how names of columns.

$cmd = new-object system.data.sqlclient.sqlcommand($sqlquery, $connection) $connection.open() $reader = $cmd.executereader() $reader.fieldcount   #returns number of columns --> 2 

use sqldataadapter.fill() instead of reader. way can have script populate datatable object retain column names:

$cmd = new-object system.data.sqlclient.sqlcommand($sqlquery, $sqlconnection) $sqlconnection.open()  # create adapter, associate command $adapter = [system.data.sqlclient.sqldataadapter]::new($cmd)  # create empty datatable $datatable = new-object system.data.datatable  # fill datatable based on query $adapter.fill($datatable)  $datatable.rows[0] # returns datarow object correct column names 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -