StoredProcedure参数数量不正确(C#)

时间:2016-11-07 09:17:52

标签: c# mysql stored-procedures

我有一个StoredProcedure我就是这样创建的;

CREATE DEFINER=`mysqladmin`@`%` PROCEDURE `Alerts_GetAlerts`(IN managerID INT)
BEGIN
    SELECT ID, Type, EmpID, ManagerID, HolID
    FROM Alerts
    WHERE ManagerID = managerID;
END$$
然后我尝试从我的C#代码中调用它,就像这样;

using (var con = new MySqlConnection(MySQLConStr))
{
    con.Open();
    using (MySqlCommand cmd = new MySqlCommand("Alerts_GetAlerts", con))
    {
        cmd.Parameters.AddWithValue("@managerID", managerID);
        using (var dataReader = cmd.ExecuteReader())
        {
            while (dataReader.Read())
            {
                var alert = new AlertsModel
                {
                    ID = Convert.ToInt32(dataReader[0]),
                    Type = Convert.ToInt32(dataReader[1]),
                    ManagerID = Convert.ToInt32(dataReader[2]),
                    EmployeeID = Convert.ToInt32(dataReader[3]),
                    HolidayID = Convert.ToInt32(dataReader[4]),
                };
                AllAlerts.Add(alert);
            }
        }
    }
    return AllAlerts;
}

但是我不断得到,Incorrect number of arguments for PROCEDURE sdcdatabase.Alerts_GetAlerts; expected 1, got 0即使对我来说,似乎我正在通过managerID参数;

cmd.Parameters.AddWithValue("@managerID", managerID);

我哪里错了?

1 个答案:

答案 0 :(得分:-1)

试试这段代码

using (var con = new MySqlConnection(MySQLConStr))
{
    con.Open();
    using (MySqlCommand cmd = new MySqlCommand("Alerts_GetAlerts(@managerID)", con))
    {
        cmd.Parameters.AddWithValue("@managerID", managerID);
        using (var dataReader = cmd.ExecuteReader())
        {
            while (dataReader.Read())
            {
                var alert = new AlertsModel
                {
                    ID = Convert.ToInt32(dataReader[0]),
                    Type = Convert.ToInt32(dataReader[1]),
                    ManagerID = Convert.ToInt32(dataReader[2]),
                    EmployeeID = Convert.ToInt32(dataReader[3]),
                    HolidayID = Convert.ToInt32(dataReader[4]),
                };
                AllAlerts.Add(alert);
            }
        }
    }
    return AllAlerts;
}