sql server 2008 - Fetch SQL data? -
i want fetch data table when run powershell script. have written following script. reason displaying field counts , not actual data.
my script:
add-pssnapin sqlserverprovidersnapin100 add-pssnapin sqlservercmdletsnapin100 $db = get-content c:\users\riteshthakur\desktop\test.txt | select -index 0 $table = get-content c:\users\riteshthakur\desktop\test.txt | select -index 1 $sqlconnection = new-object system.data.sqlclient.sqlconnection $sqlconnection.connectionstring = "server=.;database=$db;integrated security=true" $sqlconnection.open() $sqlcmd = new-object system.data.sqlclient.sqlcommand $sqlcmd.commandtext = "select * $table" $sqlcmd.connection = $sqlconnection $x = $sqlcmd.executereader() write-output $x $sqlconnection.close()
once created reader need read data:
$x = $sqlcmd.executereader() while ($x.read()) { $x.getvalue(0) $x.getvalue(1) ... } $x.close()
or use sqldataadapter
instead of reader:
$sqlcmd = $sqlconnection.createcommand() $sqlcmd.commandtext = "select * $table" $sqladapter = new-object data.sqlclient.sqldataadapter $sqlcmd $tbl = new-object data.datatable $sqladapter.fill($tbl) $tbl | format-table -autosize
Comments
Post a Comment