Solved

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

Posted on 2014-07-29
1
636 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Utilizing an array to gracefully append to a list of EmailAddresses
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now