gpt4 book ai didi

c# - MySQL 存储过程 : Works manually & not with C#

转载 作者:行者123 更新时间:2023-11-30 22:20:52 27 4
gpt4 key购买 nike

我是第一次使用存储过程。我很难找出这背后的问题。如果我从 PhpMyAdmin 手动执行存储过程,它工作正常。但是当我将它与 C# 程序一起使用时。它显示错误。

存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `Save`(INOUT `id` INT, IN `cId` INT, IN `date` DATE, IN `randomNum` INT, IN `debitAmount` VARCHAR(20), IN `descr` VARCHAR(255))
NO SQL

BEGIN
INSERT INTO dc (status, dcDate, clientId) VALUES ('1', date, cId);
SET id = last_insert_id();

INSERT INTO bill (dcId, clientId, billDate, referenceNo) VALUES (
id,
clientId,
date,
randomNum
);

INSERT INTO ledger (clientId, debit, ledgerDate, description, dcId, referenceNo)
VALUES (
cId,
debitAmount,
date,
descr,
id,
randomNum
);


SELECT @id;

END

当我使用这些参数执行此过程时:

var reader = connection.StoredProcedureInOut("Save", new String[] { "0", theDate, clientId, randomNumber.ToString(), totalDebit.ToString(), description }, new String[] { "id", "date", "cId","randomNum", "debitAmount", "descr" });            

定义是:

 public MySqlDataReader StoredProcedureInOut(String procedureName, String[] values, String[] keys)
{
openConnection();
MySqlDataReader reader;
try
{
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < values.Length; i++)
{
cmd.Parameters.AddWithValue("@" + keys[i], values[i]);

}
reader = cmd.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
reader = null;
}
return reader;
}

我收到这条消息:

OUT or INOUT argument 1 for routine noorani.Save is not a variable or NEW pseduo-variable in Before trigger

最佳答案

通过修改代码解决了这个问题:

var reader = connection.StoredProcedureInOut("Save", new String[] {theDate, clientId, randomNumber.ToString(), totalDebit.ToString(), description }, new String[] {"date", "cId","randomNum", "debitAmount", "descr" }, new String[] {"id"});            

定义:

public String[] StoredProcedureInOut(String procedureName, String[] values, String[] keys, String[] outputParameters)
{
openConnection();
String[] newlyInsertedID = new String[outputParameters.Length];
try
{
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
cmd.CommandType = CommandType.StoredProcedure;

for (int i = 0; i < outputParameters.Length; i++)
{
cmd.Parameters.AddWithValue("@" + outputParameters[i], "0");
var parameterName = "@"+outputParameters[i];
cmd.Parameters[parameterName].Direction = ParameterDirection.Output;
}

for (int i = 0; i < values.Length; i++)
{
cmd.Parameters.AddWithValue("@" + keys[i], values[i]);
var parameterName = "@" + keys[i];
cmd.Parameters[parameterName].Direction = ParameterDirection.Input;
}
//reader = cmd.ExecuteReader();
cmd.ExecuteNonQuery();
for(int i = 0; i < outputParameters.Length; i++)
{
var parameterName = "@"+outputParameters[i];

newlyInsertedID[i] = cmd.Parameters[parameterName].Value.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//reader = null;
}
closeConnection();
return newlyInsertedID;
}

代码中缺少的是方向。此外,这是一个通用代码,可以为所有类型的 Out/InOut 存储过程执行。

希望对您有所帮助。

关于c# - MySQL 存储过程 : Works manually & not with C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36667031/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com