我正在编写一个简单的查询函数,以使我的脚本高效且简单,因为我将来开始编写更多查询。
Function Query($Query) {
$connectionString = "Provider=msolap;Data Source=$Server;Initial Catalog=$Database;"
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand
$command.Connection = $connection
$command.CommandText = $Query
$connection.Open()
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter #$command
$adapter.SelectCommand = $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)
$connection.Close()
$dataSet.Tables[0]
}
询问
SELECT [SESSION_ID], [SESSION_SPID]
FROM $SYSTEM.DISCOVER_SESSIONS";
我收到此错误:
使用“1”参数调用“Fill”的异常:“Query (1, 41) The syntax for '.' 不正确。(从 .DISCOVER_SESSIONS 中选择 [SESSION_ID]、[SESSION_SPID])。”
+ [void] $adapter.Fill($dataSet)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (: ) [], MethodInvocationException
+ FullyQualifiedErrorId : OleDbException
我错过了什么吗?我遵循每个 MSDocs 的语法:
我实际上在没有创建查询函数的情况下成功地尝试了它并显示了结果。不知何故,将其作为查询功能不起作用...