如何使用来自 ASP.NET 的多个输入值调用 MySQL 存储过程

Moh*_*ela 2 .net c# mysql asp.net stored-procedures

这是我的 MySQL 存储过程。

    create procedure InsertIntotblStudentProc (PStudentId VARCHAR(10), PStudentName VARCHAR(10))
    begin
    insert into tblStudent (StudentId, StudentName) values (PStudentId, PStudentName);
end;
Run Code Online (Sandbox Code Playgroud)

这是我的 ASP 代码。

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

我在这里停下来,因为我想用两个参数调用过程,而我的另一个参数在 TextBox2 中。

帮我提建议。

小智 5

可以在 command.Parameters 中添加多个参数,同样参考下面的代码。

        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();
    }
Run Code Online (Sandbox Code Playgroud)