我正在寻找使用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”参数调用“Fill”的异常:“Microsoft Access数据库引擎找不到输入表或查询''实验手册''。确保它存在并且其名称拼写正确。“
答案 0 :(得分:1)
诀窍是在查询名称前添加命令'Execute',并在查询名称周围使用方括号。
$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();