Link to home
Start Free TrialLog in
Avatar of whorsfall
whorsfallFlag for Australia

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial