Solved

How to add a record to a sql database using powershell using ADO

Posted on 2014-07-29
1
676 Views
Last Modified: 2014-08-02
Hi,

I am looking for some powershell code to add a record to a SQL database.
Preferbly I don't want to use the SQL insert statement to messy with the formatting.

So is there a way I can do this with a ADO recordset or datatable etc?

Thanks,

PS. Adding another question for a sql update btw. :)


Ward
0
Comment
Question by:whorsfall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40226399
you general ADO question is this one (where I posted):
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28485997.html

the powershell technique is like this (except that you don't use the .AddNew, obviously):
http://blogs.technet.com/b/heyscriptingguy/archive/2008/01/15/how-can-i-add-a-record-to-a-database-using-windows-powershell.aspx
$adOpenKeyset = 3
$adLockOptimistic = 3

$objConnection = New-Object -com "ADODB.Connection"
$objRecordSet = New-Object -com "ADODB.Recordset"

$objConnection.Open("<your connection string")

$objRecordset.Open("Select * From yourtable WHere keyfield = xxx", $objConnection,$adOpenKeyset ,$adLockOptimistic)

$objRecordSet.Fields.Item("ComputerName").Value = "atl-ws-001"
$objRecordSet.Fields.Item("SerialNumber").Value = "192ATG43R"
$objRecordSet.Update()

$objRecordSet.Close()
$objConnection.Close()

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
A brief introduction to what I consider to be the best editor for PowerShell.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question