How to add a row in Powershell System.Data.Datatable

27 July,2017 by Jack Vamvas

Powershell System.Data.Datatable is one of my favourite methods in powershell scripting to manage data sets. It works in-memory – which makes it very fast , and is versatile when moving data around different data sources.

I’ve used it extensively in different Powershell scripts . One of its particular strengths is working with SQL Server and the capacity to load recordsets.

There is a fair amount of control available to the programmer when using the System.Data.Datatable. For example ,

-To instantiate a new Powershell object

$dt = new-object "System.Data.DataTable"

--To create new columns – in this example I’m using 2

$col1 = New-Object system.Data.DataColumn ColumnName1,([string])

$col2 = New-Object system.Data.DataColumn ColumnName2,([string])

$dt.columns.add($col1)

$dt.columns.add($col2)  

 

--To create a new row

$row = $dt.NewRow()

--Add some values to the new row

$row.ColumnName1 = “Guitar “

$row.ColumnName2 = “ Carlos Santana”

--Add the row to the System.Data.Datatable

$dt.Rows.Add($row)

 

SIMPLE! This is just a basic example of instantiating a Powershell new object of System.Data.Datatable .  You  can progress by  adapting into loops and other constructs .

here are some examples of Powershell scripts utilising System.Data.Datable, in different situations, trying to solve different problems.

SQL Server – Export Excel data to SQL Server with Powershell (SQL ...

Powershell Add-Content – How to add another query result to an existing html report

 

 


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to add a row in Powershell System.Data.Datatable


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer