Background
Yesterday, I found myself struggling with a database project.
I thought let me capture some metadata, view it in a grid view and I will have more to go with.
Clipboard
Captured the data into a data table, pipelined it into GridView.
Got a good thing going.
As always got greedy.
Thought to capture in clipboard.
Code
<# powershell -- how do I create and populate a DataTable from a CSV file? https://docs.microsoft.com/en-us/answers/questions/333793/powershell-how-do-i-create-and-populate-a-datatabl.html Question:- Christian Bahnsen Answer:- Andreas Baumgarten #> # Define the DataTable Columns [System.Data.DataTable] $table = $null; $table = New-Object system.Data.DataTable 'TestDataTable' $columnFirstname = New-Object system.Data.DataColumn FirstName,([string]); $table.columns.add($columnFirstname) $columnLastname = New-Object system.Data.DataColumn LastName,([string]); $table.columns.add($columnLastname) $columnID = New-Object system.Data.DataColumn ID,([int]); $table.columns.add($columnID) # Add a DataTable row $row = $table.NewRow() $row.FirstName= ("Bill") $row.LastName= ("Gates") $row.ID= [int](123456) $table.Rows.Add($row) $table | Out-GridView -PassThru | Set-Clipboard
GridView
Everything works well.
Your Data table is populated.
The data is shown in a grid view.
You select all of the rows in the Grid View.
Engage the OK button on the grid view.
Upon the grid view closing, you paste your captured data into a document.
Surprise.
All you see is
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
Code Sharing
GitLab
Snippets
Powershell:- DataTable & GridViewLink
Dedication
Will dedicate this post to Christian Bahnsen and Andreas Baumgarten.
QA
Christian Bahnsen
Christian Bahnsen for asking a nice straight forward question.
It goes…
Image
Text
I want to try to do a bulk insert to a sql table.
I found an article at bulk-copy-data-sql-server-powershell
that discusses using a DataTable as a source. I’ve never worked with DataTables before.
I’ve saved the data as a CSV that has the same column names as the destination table.
So it looks like I need to create the DataTable first:
$a = New-Object System.Data.DataTable
$a | Get-Member
But when I run Get-Member it tells me “You must specify an object for the Get-Member cmdlet.” I thought the New-Object statement would have created an object.
Then I need to populate the object. I was hoping something like
Get-Content filename.csv could be piped into the DataTable.
Do I need to define columns for the DataTable first?
Then how can I populate the DataTable from the CSV?
Thanks in advance for any assistance.
Christian Bahnsen
Andreas Baumgarten
Salute
Just now discovered that Andreas Baumgarten is a Microsoft MVP.
So you know you gotta to Lean Back.
#1 Draft Pick Like Eli Manning.
Summary
Unfortunately, as good a thing as the data table is, it does not interact with a grid view in such a way that one can capture the raw data; less to talk of its richness.
You will have to try other things to capture your data in a clipboard.