Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell extract a value from a table

I'm still pretty new into Powershell and this has taken way too much time, so HELP! (please)

I have a script that starts out by extracting a value from a SQL Server table. The extract works by populating a "table" via:

$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)

Where I run into a problem is getting the single value out of $DataSet. The value is a date time stamp that I need. Referencing $DataSet.Tables[0] shows me the date time stamp with a heading of Column1. I've tried all kinds of casting, ToString(), etc. The results are either a display of data types (e.g. System.Data.DataRow) or any number of errors. How do I get the value into a variable that is recognized as a date time?

TIA,

John

like image 888
John Avatar asked Sep 12 '25 20:09

John


2 Answers

A DataSet in ADO.NET contains multiple tables. What you have is just giving you the first (only) table in your dataset. You need to specify which row and column to return to get a single value. You would have to do something like this if you are looking for a single value:

$value = $DataSet.Tables[0].Rows[0]["Column1"]
like image 144
db_brad Avatar answered Sep 14 '25 19:09

db_brad


Select the appropriate row from the Rows property and then grab the value by column name:

$row1column1value = $dataset.Tables[0].Rows[0].Column1
like image 23
Mathias R. Jessen Avatar answered Sep 14 '25 20:09

Mathias R. Jessen