Powershell - 调用存储过程 - 与System.Data.SqlClient.SqlConnection一起使用但不与System.Data.Odbc.OdbcConnection一起使用

时间:2014-02-27 21:16:12

标签: sql tsql powershell

我有一个奇怪的问题。我有一个PowerShell脚本,在设计为使用直接SQLConnection后,我一直试图使用ODBC连接。

当$ odbc为false时,此代码运行正常,当它为$ true时,我收到以下错误:

Exception calling "Fill" with "1" argument(s): "ERROR [42000] [Microsoft][SQL Server Native Client 10.0][SQL Server]Procedure or function 'External_DeficiencyListAll' expects parameter '@startTime', which was not supplied." At C:\Users\afreeberg\Documents\Hyland\Trinitas\Deficiency Polling Powershell\Deficiency_Polling.ps1:317 char:19 +     $dataAdapter.Fill <<<< ($results[0]) | Out-Null     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException     + FullyQualifiedErrorId : DotNetMethodException

以下是代码:

if ($odbc)
{
    LogWrite("Using ODBC Connection")
    $odbcConnection = New-Object System.Data.Odbc.OdbcConnection
    $odbcConnection.ConnectionString = $odbcString
    $odbcCommand = $odbcConnection.CreateCommand()
    $odbcCommand.CommandType = [System.Data.CommandType]::StoredProcedure
    $odbcCommand.CommandText = "[dbo].[External_DeficiencyListAll]"
    $odbcCommand.Parameters.AddWithValue("@startTime", $startTime) | Out-Null
    $outParameter = New-Object System.Data.Odbc.OdbcParameter
    $outParameter.ParameterName = "@nowTime"
    $outParameter.Direction = [System.Data.ParameterDirection]::Output
    $outParameter.DbType = [System.Data.DbType]::DateTime
    $odbcCommand.Parameters.Add($outParameter) | Out-Null
    $dataAdapter = New-Object System.Data.ODBC.OdbcDataAdapter $odbcCommand

}
else
{
    LogWrite ("Using SQL Connection")
    $sqlConnection = New-Object System.Data.sqlClient.SqlConnection
    $sqlConnection.ConnectionString = $connString
    $sqlCommand = $sqlConnection.CreateCommand()
    $sqlCommand.CommandType = [System.Data.CommandType]::StoredProcedure
    $sqlCommand.CommandText = "[dbo].[External_DeficiencyListAll]"
    $sqlCommand.Parameters.AddWithValue("@startTime", $startTime) | Out-Null
    $outParameter = New-Object System.Data.SqlClient.SqlParameter
    $outParameter.ParameterName = "@nowTime"
    $outParameter.Direction = [System.Data.ParameterDirection]::Output
    $outParameter.DbType = [System.Data.DbType]::DateTime
    $sqlCommand.Parameters.Add($outParameter) | Out-Null
    $dataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $sqlCommand
}

LogWrite ("Querying for deficiencies updated since: " + $startTime)
#Initialize Dataset object to hold results from stored procedure query
$results = @()
$results += @(New-Object System.Data.Dataset)


#Execute query using $dataAdapter and filling $results
$dataAdapter.Fill($results[0]) | Out-Null

$results += @($sqlCommand.Parameters["@nowTime"].Value)

#return $results
$results

重要参数:

$odbcString = "DSN=defPolLTest" Corresponds to an ODBC connection set up to point to the SQL database referred to in $connString

$connString = 'Server=ONE-017125\SQLEXPRESS;Database=DeficiencyPolling;User Id=dpolling;Password=password;'

$startTime corresponds to the last run time of the script, stored in the registry. Is a DateTime value.

因此,在使用SQLConnection的情况下,可以识别参数,但在使用OdbcConnection时它不会...

0 个答案:

没有答案