Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Loop Datareader and create DataTable in Powershell

First of all let me thank those people that answered my previous questions. You guys are Awesome!!!

Here is my question: I like to query my sql server Stored procedure and return a datareader. However I want to create a Table out of it. That table I will use to load excel using the new Powershell OpenXML commandlets. The code fails when I try to build the DataTable. I don't think I am loding the new-object "System.Object[]" correctly. Here is what I got so far:

$sqlConnection = new-object System.Data.SqlClient.SqlConnection "server=localhost;database=Demo;Integrated Security=sspi"
$sqlConnection.Open()

#Create a command object
$sqlCommand = $sqlConnection.CreateCommand()
$sqlCommand.CommandText = "EXEC Demo.usp_GetTableValueParameter_Data"

#Execute the Command
$sqlReader = $sqlCommand.ExecuteReader()

#Parse the records

$sqlReader | &{ begin{$values = new-object "System.Object[]" $sqlReader["Name"], $sqlReader["Level_Desc"], $sqlReader["Level"]} process {$_.GetValues($values); $datatable.Rows.Add($values)}}

##$datatable | format-table -autosize

# Close the database connection
$sqlConnection.Close()

#STARTING OPENXML PROCESS
#----------------------------
$xlsFile = "C:\Temp\Data.xlsx"
$datatable | Export-OpenXmlSpreadSheet -OutputPath $xlsFile  -InitialRow 3
like image 528
Tor Storli Avatar asked Jul 26 '09 15:07

Tor Storli


1 Answers

Translating Mladen's answer into PowerShell is pretty straight forward:

$sqlConnection = new-object System.Data.SqlClient.SqlConnection "server=localhost;database=Demo;Integrated Security=sspi"
$sqlConnection.Open()

#Create a command object
$sqlCommand = $sqlConnection.CreateCommand()
$sqlCommand.CommandText = "EXEC Demo.usp_GetTableValueParameter_Data"

#Execute the Command
$sqlReader = $sqlCommand.ExecuteReader()

$Datatable = New-Object System.Data.DataTable
$DataTable.Load($SqlReader)

# Close the database connection
$sqlConnection.Close()

#STARTING OPENXML PROCESS
#----------------------------
$xlsFile = "C:\Temp\Data.xlsx"
$datatable | Export-OpenXmlSpreadSheet -OutputPath $xlsFile  -InitialRow 3

However, if you just need a DataTable back, you don't need to call ExecuteReader on the command, you could create a DataAdapter and use that to fill the DataTable:

$sqlConnection = new-object System.Data.SqlClient.SqlConnection "server=localhost;database=Demo;Integrated Security=sspi"
$sqlConnection.Open()

#Create a command object
$sqlCommand = $sqlConnection.CreateCommand()
$sqlCommand.CommandText = "EXEC Demo.usp_GetTableValueParameter_Data"

$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $sqlcommand
$dataset = New-Object System.Data.DataSet

$adapter.Fill($dataSet) | out-null

# Close the database connection
$sqlConnection.Close()

$datatable = $dataset.Tables[0]  

#STARTING OPENXML PROCESS
#----------------------------
$xlsFile = "C:\Temp\Data.xlsx"
$datatable | Export-OpenXmlSpreadSheet -OutputPath $xlsFile  -InitialRow 3
like image 189
Steven Murawski Avatar answered Nov 15 '22 06:11

Steven Murawski