Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-07-29
1
Medium Priority
?
719 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 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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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