0

我正在寻找使用 Microsoft.ACE.OLEDB.12.0 命令对象执行 MS Access 命名查询的语法。

我看到很多使用表的示例,但还没有用于查询。将表名换成查询名似乎不起作用。即从'myquery'中选择*

这是我的代码片段:

$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.Open();

$OleDbCmd.Connection = $OleDbConn;
$OleDbCmd.CommandText = "'myQuery'"; # name query in MS Access db
$OleDbCmd.CommandType = [System.Data.CommandType]::StoredProcedure;
$OleDbAdapter.SelectCommand = $OleDbCmd;

$RowsReturned = $OleDbAdapter.Fill($DataTable);
Write-Host $RowsReturned;

错误:使用“1”参数调用“填充”的异常:“Microsoft Access 数据库引擎找不到输入表或查询“实验室手册”。确保它存在并且其名称拼写正确。”

4

1 回答 1

1

诀窍是在查询名称之前附加命令“执行”,并在查询名称周围使用方括号。

$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();
于 2012-10-16T18:38:13.130 回答