Working with Basic .NET DataSets and DataTables in PowerShell

This is mostly for my reference, but you can use it, too :)

Create Basic Datatable

dt

Create Basic DataSet (Collection of Tables)

dt

Selecting

Create Datatable from CSV

Using this method, you can add 140k rows a second.

Managing Duplicates

Create new table using another datatable’s schema

Filling DataTables and DataSets with data from SQL Server

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, and holds a number of certifications, including those relating to SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in PowerShell, SQL Server
12 comments on “Working with Basic .NET DataSets and DataTables in PowerShell
  1. Colin says:

    Of all the googled PS DataSet explanations, this one is far and away the best.

    Thanks Chrissy.

  2. SB says:

    Colin; seconded.

  3. robduncan says:

    Great article – would love to see examples of loading xml files into datatables

  4. Karl says:

    Would like to see a follow-up to:
    Filling DataTables and DataSets with data from SQL Server then update
    the database from which the data was fetched from.

  5. Keng Onn says:

    Hi, Thanks for sharing your expertise, Chrissy, really appreciate it! :) Just one question though, how do you cater for nulls in the source data? I was using the code from your post at https://gallery.technet.microsoft.com/scriptcenter/Import-Large-CSVs-into-SQL-216223d9 for bulk insert, but got stuck when I tried to insert data like this “abc,d,,f,,e” where I expect to see the second last column of that table to be null instead of an empty string. I tried using [DBNull]::Value but that didn’t work :(. Would appreciate hearing whatever insight you have on this. Thanks! Cheers, Keng Onn.

  6. Brain Zender says:

    Yep. I agree with the comments above — this is a clean, clear explanation. Laissez les bons temps rouler, cher!

  7. J Spivey says:

    Thank you for this excellent write up and example. I wish MicroSoft would provide such clear examples. I visited many other pages before finding this one, it was very helpful for a problem I was trying to solve.

  8. Michael Osmond says:

    Thank you. Very useful.

  9. Photonn says:

    Really useful. Thanks a lot!

  10. Steve Schaub says:

    Very useful – any chance you could give an example of selecting data from different sources into datatables and then joining those tables? Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*