Powershell scripting to insert records in MSSQL through an SMO Connection

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
dakota
Posts: 2
Last visit: Thu Mar 23, 2017 3:48 am

Powershell scripting to insert records in MSSQL through an SMO Connection

Post by dakota »

I have a set of records in $result and i want to insert these records in a table available in MSSQL through an SMO Connection having authentication(username and password). I have tried many things but not able to insert it into the table.How to execute the above scenario? Can someone help me with scripting?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell scripting to insert records in MSSQL through an SMO Connection

Post by jvierra »

We wouldn't use SMO to update a table, SMO is designed to be used for managing a database. For updates we would use the OleDBClient.

It takes about 20 lines of standard SQL code to update a table from an object.

There are dozens of blog examples available if you search. Here is one example. Change the provider to MySql and it should work:
  1. $DataSource='c:\test\testdb.accdb'
  2. $conStr="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=$DataSource"
  3. $conn=New-Object System.Data.OleDb.OleDbConnection($conStr)
  4.  
  5. $conn.Open()
  6. $adcmd = $conn.CreateCommand()
  7. $adcmd.CommandText='select * from act3'
  8. $oleadptr=New-Object System.Data.OleDb.OledbDataAdapter($adcmd)
  9. $cb=New-Object System.Data.OleDb.OleDbCommandBuilder($oleadptr)
  10.  
  11. $dt = New-object System.Data.DataTable
  12. $oleadptr.Fill($dt)
  13.  
  14. $id=56
  15. $displayName='Hello'
  16. for($i=0; $i -lt 10;$i++){
  17.     $row=$dt.NewRow()
  18.     $row.DisplayName=$displayName
  19.     $row.ID=$id
  20.     $row=$dt.Rows.Add($row)
  21. }
  22. $oleadptr.Update($dt)
  23. $conn.Close()
User avatar
dakota
Posts: 2
Last visit: Thu Mar 23, 2017 3:48 am

Re: Powershell scripting to insert records in MSSQL through an SMO Connection

Post by dakota »

Hi jvierra,

Thanks for the reply!!!
But I need scripting for MSSQL and not MySQL with Authentication. If you could guide me on this , then please suggest?

Regards,
Dakota
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell scripting to insert records in MSSQL through an SMO Connection

Post by jvierra »

Change OleDb to Sql and it is for SQS.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell scripting to insert records in MSSQL through an SMO Connection

Post by jvierra »

Here is an example of loading into a sql table:
Attachments
ImportFrom-CsvToSQL.ps1
(3.41 KiB) Downloaded 153 times
This topic is 7 years and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked