mysql执行带参数的sql_mysql - 使用Powershell中的参数执行sql过程

我对Powershell完全陌生,因此我对如何调用带有参数的SQL过程有些困惑。我已经成功打开了到数据库的连接,并且设法获得了一个不带参数的过程,所以我知道连接很好。

添加参数并运行查询的代码如下:$dateToUse = Get-Date -f yyyy/MM/dd

$MysqlQuery.CommandText = "GetJourneyByDepartureDate"

$MysqlQuery.Parameters.AddWithValue("_departureDate", $dateToUse)

$queryOutput = $MysqlQuery.ExecuteReader()

每当我尝试运行脚本时,都会出现错误提示

Incorrect number of arguments for PROCEDURE dbo.GetJourneyByDepartureDate; expected 1, got 0

我四处寻找试图找到解决方案的方法,但是对Powershell的了解不足,无法知道哪种解决方案可能是正确的。

另外,我无法发布SQL查询,但是通过在HeidiSQL中运行查询以手动传递论点,我已经设法多次运行了该过程

编辑:

我现在已经稍微更改了代码,现在看起来像这样:

$MysqlQuery.CommandText = "GetJourneyByDepartureDate"

$MysqlQuery.Parameters.Add("@_departureDate", [System.Data.SqlDbType]::Date) | out-Null

$MysqlQuery.Parameters['@_departureDate'].Value = $dateToUse

$parameterValue = $MysqlQuery.Parameters['@_departureDate'].value

Write-Host -ForegroundColor Cyan -Object "$parameterValue";

$queryOutput = $MysqlQuery.ExecuteReader()

我在控制台上的Write-Host行中获得了$ dateToUse值输出,但是我仍然收到与以前相同的不正确数量的参数错误。 SP声明如下:

CREATE PROCEDURE `GetJourneyByDepartureDate`(IN `_departureDate` DATE) READS SQL DATA

最佳答案

最后,我发现我需要将CommandType设置为StoredProcedure,还需要添加参数,但是我错过了方向,显然我必须在'@'之后添加一个空格,但是我不确定为什么。我的解决方案如下:$MysqlCommand = New-Object MySql.Data.MySqlClient.MySqlCommand.Connection = $connMySQL #Create SQL command

$MysqlCommand.CommandType = [System.Data.CommandType]::StoredProcedure; #Set the command to be a stored procedure

$MysqlCommand.CommandText = "GetJourneyByDepartureDate"; #Set the name of the Stored Procedure to use

$MysqlCommand.Parameters.Add("@ _departureDate", [System.Data.SqlDbType]::Date) | out-Null; #Set the input and output parameters

$MysqlCommand.Parameters['@ _departureDate'].Direction = [system.data.ParameterDirection]::Input; #Set the _departureDate parameter to be an input parameter

$MysqlCommand.Parameters['@ _departureDate'].Value = $dateToUse; #Set the _departureDate parameter value to be dateToUse

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值