Solved

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

Posted on 2014-07-29
1
669 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
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

756 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