How To Execute MS Access Query with OLEDB.12

后端 未结 1 1228
灰色年华
灰色年华 2021-01-28 07:04

I am looking for the syntax for executing MS Access named query using Microsoft.ACE.OLEDB.12.0 command object.

I see lots of examples using tables but non for queries

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-28 07:28

    The trick was to append the command 'Execute' before the query name and use square brackets around the query name.

    $OleDbConn = New-Object "System.Data.OleDb.OleDbConnection";
    $OleDbCmd = New-Object "System.Data.OleDb.OleDbCommand";
    $OleDbAdapter = New-Object "System.Data.OleDb.OleDbDataAdapter";
    $DataTable = New-Object "System.Data.DataTable";
    
    $OleDbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\temp\labmanual.mdb;";
    $OleDbCmd.Connection = $OleDbConn;
    $OleDbCmd.CommandText = "Execute [myQuery]";
    
    $OleDbAdapter.SelectCommand = $OleDbCmd;
    
    $OleDbConn.Open();
    $RowsReturned = $OleDbAdapter.Fill($DataTable);
    
    
    Write-Host $RowsReturned;
    
    $OleDbConn.Close();
    

    0 讨论(0)
提交回复
热议问题