SQL dataset update using PowerShell

Hello,

I am reading an whole dataset from SQL table:

$query="select * from myTable"

$Con = New-Object System.Data.SqlClient.SQLConnection
$Con.ConnectionString =  "Server=$svr;Database=$dbname;Integrated
Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $Query
$SqlCmd.Connection = $Con
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$Con.Close()
$DataSet.Tables[0] | format-table -auto

Now, I would like to make some changes in $DataSet:

$DataSet.Tables[1].Col[1]=1
$DataSet.Tables[1].Col[2]=2

And now I would like to put back the whole $DataSet to SQL Table.

How do I manage it, please?

============

Regards r.


  • Edited by Roman_i 17 hours 44 minutes ago
July 30th, 2015 9:27am

You want to do a batch update. 

https://msdn.microsoft.com/en-us/library/at8a576f(v=vs.110).aspx

Basically don't close the adapter and use the dataset update method to post changes back.

$sqldapter.Update($dataset)

Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 11:28am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics