How to call MySQL stored procedure with multiple input values from ASP.NET

怎甘沉沦 提交于 2021-02-10 06:39:05

问题


This is my MySQL stored procedure.

    create procedure InsertIntotblStudentProc (PStudentId VARCHAR(10), PStudentName VARCHAR(10))
    begin
    insert into tblStudent (StudentId, StudentName) values (PStudentId, PStudentName);
end;

Here's my ASP code.

   `MySqlCommand cmd = new MySqlCommand("InsertIntotblStudent", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("PStudentId", TextBox1.Text);`

I stopped here as I want to call procedure with two parameters and my other parameter is in TextBox2.

Help me with suggestions.


回答1:


You can add mutiple parameters in command.Parameters, refer the below code for the same.

        var connectionString = ""; // Provide connecction string here.
    using (var connection = new MySqlConnection(connectionString))
    {
        MySqlCommand command = new MySqlCommand("InsertIntotblStudent", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new MySqlParameter("PStudentId", TextBox1.Text));
        command.Parameters.Add(new MySqlParameter("PStudentName", TextBox2.Text));
        command.Connection.Open();
        var result = command.ExecuteNonQuery();
        command.Connection.Close();
    }



回答2:


consider using list of MySqlParameter like this:

    var sqlParameters = new List<MySqlParameter>();
    sqlParameters.Add(new MySqlParameter { MySqlDbType = MySqlDbType.Int32, ParameterName = "@valuename", Value = textbox1 });
        .
        .
    cmd.Parameters.AddRange(sqlParameters.ToArray());


来源:https://stackoverflow.com/questions/43730890/how-to-call-mysql-stored-procedure-with-multiple-input-values-from-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!