简体   繁体   中英

Unable to execute MySql stored procedure from c#

In MySql:

DELIMITER //

DROP PROCEDURE IF EXISTS `testdb`.`Check_UserId_Sproc` //
CREATE PROCEDURE `testdb`.`Check_UserId_Sproc` (IN User_Id NVARCHAR(100))
BEGIN
 select count(*) from demo_user where userid = User_Id;
END //

DELIMITER ;

In C#:

public DataTable ExecuteParameterizedSelectCommand(string CommandName, CommandType cmdType,MySqlParameter[] param)
        {
            DataTable table = new DataTable();
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using(MySqlConnection con = new MySqlConnection(CS))
            {
                using (MySqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = CommandName;
                    cmd.Parameters.AddRange(param);

                    try
                    {
                        if (con.State != ConnectionState.Open)
                        {
                            con.Open();
                        }

                        using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                        {
                            da.Fill(table);
                        }
                    }
                    catch
                    {
                        throw;
                    }

                    return table;
                }
            }
        }


    public DataTable checkExistingUserId()
            {
                MySqlDBHelper oHelper = new MySqlDBHelper();
                MySqlParameter[] parameters = new MySqlParameter[]
                {
                    new MySqlParameter("User_Id", 'DemoId')
                };
                return oHelper.ExecuteParameterizedSelectCommand("Check_UserId_Sproc", CommandType.StoredProcedure, parameters);
            }

When I try to execute the checkExistingUserId() , I get following exception:

Incorrect number of arguments for PROCEDURE testdb.Check_UserId_Sproc; expected 1, got 0

May be I am doing a silly mistake but I am not able to figure it out. I am new to mysql and trying to work around it.

When I debug the array contains the parameter as seen in below image, but it is not collected by the SP.

在此处输入图片说明

Thanks in advance

In your code:

cmd.CommandType = CommandType.Text;

should be

cmd.CommandType = cmdType;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM