Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-07-29
1
Medium Priority
?
764 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Loops Section Overview

773 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