MySQL存储过程:手动工作&不是用C#

时间:2016-04-16 16:57:55

标签: c# mysql stored-procedures

我第一次使用存储过程。我很难搞清楚这背后的问题。 如果我手动执行 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

1 个答案:

答案 0 :(得分:0)

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

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存储过程。

希望这有帮助。