Solved

Trying to do a SQL insert  from powershell to a MSAccess database

Posted on 2013-12-16
1
835 Views
Last Modified: 2014-01-06
Hi,

I am trying to do a SQL insert into MSAccess database and coming up with an error in powershell.

Do u have any suggestions on how I can fix the code below:

    clear
    $db = New-Object -com "ADODB.Connection"
    
    $db.Open("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ad\VP.accdb")    

    $sql = "insert into vp_debug (Machine_Name) Values ('Test')"
    
    $test_cmd = New-Object System.Data.OleDb.OleDbCommand


    $test_cmd.Connection = $db
    $test_cmd.CommandText = $sql

    $test_cmd.ExecuteNonQuery()

Open in new window


The error I get is:

Exception setting "Connection": "Cannot convert the "System.__ComObject" value of type "System.__ComObject#{00001550-0000-0010-8000-00aa006d2ea4}" to type "System.Data.OleDb.OleDbConnection"."
At C:\ad\Untitled1.ps1:11 char:5
+     $test_cmd.Connection = $db
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting
 
Exception calling "ExecuteNonQuery" with "0" argument(s): "ExecuteNonQuery: Connection property has not been initialized."
At C:\ad\Untitled1.ps1:14 char:5
+     $test_cmd.ExecuteNonQuery()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException
0
Comment
Question by:whorsfall
1 Comment
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39721154
Try this - I don't think your connection definition is quite right:

$cn = New-Object data.OleDb.OleDbConnection "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ad\VP.accdb"
$sql = "insert into vp_debug (Machine_Name) Values ('Test')"
    
$testcmd = New-Object System.Data.OleDb.OleDbCommand $sql
$testcmd.connection = $cn
$testcmd.ExecuteNonQuery()

Open in new window

0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

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…
Set OWA language and time zone in Exchange for individuals, all users or per database.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

837 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