我想在QA环境的MySQL 5.6服务器上执行MySQL存储过程。这是通过Azure固定线实施持续交付的一部分。
例如:我的.sql
文件具有多个存储过程,这些存储过程已由MySQL Workbench成功执行,但是我想通过PowerShell执行的文件相同。
.sql内容:
DROP procedure IF EXISTS `test`;
DELIMITER //
CREATE PROCEDURE `test` ()
BEGIN
select * from organisations;
END
//
DELIMITER ;
DROP procedure IF EXISTS `test1`;
DELIMITER //
CREATE PROCEDURE `test1` ()
BEGIN
select * from organisations;
END
//
DELIMITER ;
PowerShell脚本代码:
[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
[string]$sMySQLUserName = 'xxx'
[string]$sMySQLPW = 'xxx'
[string]$sMySQLDB = 'xxx'
[string]$sMySQLHost = 'localhost'
[string]$sConnectionString = "server="+$sMySQLHost+";port=3306;uid=" + $sMySQLUserName + ";pwd=" + $sMySQLPW + ";database="+$sMySQLDB
$oConnection = New-Object MySql.Data.MySqlClient.MySqlConnection($sConnectionString)
$Error.Clear()
try
{
$oConnection.Open()
write-host "Connection opened"
}
catch
{
write-warning ("Could not open a connection to Database $sMySQLDB on Host $sMySQLHost. Error: "+$Error[0].ToString())
}
#$oTransAction=$oConnection.BeginTransaction()
$oMYSQLCommand = New-Object MySql.Data.MySqlClient.MySqlCommand
$oMYSQLCommand.Connection=$oConnection
#$oMYSQLCommand.Transaction=$oMYSQLTransaction
#$sql = Get-Content D:\Release\ConsoleDev\SqlScript\testprocedure.sql
$sql = [io.file]::ReadAllText('D:\Release\ConsoleDev\SqlScript\testprocedure - Copy.sql')
$oMYSQLCommand.CommandText = $sql
write-host $sql
try
{
$iRowsAffected=$oMYSQLCommand.executeNonQuery()
}
catch
{
write-warning ("ERROR occured while ExecuteNonQuery")
}
# Do some Inserts or updates here and commit your changes
finally
{
$oConnection.Close()
write-host "Closing Connection"
}
PowerShell引发以下错误:
警告:ExecuteNonQuery发生错误
我认为执行DELIMITER //时遇到问题。
答案 0 :(得分:0)
我已解决以下更改的问题-1)$ oMYSQLCommand =新对象MySql.Data.MySqlClient.MySqlScript 2)$ oMYSQLCommand.Query = $ sql 3)$ oMYSQLCommand.Execute()并最终已执行并将proc存储在我的数据库中。 –