【问题标题】:How to call MySQL stored procedure with multiple input values from ASP.NET如何从 ASP.NET 调用具有多个输入值的 MySQL 存储过程
【发布时间】:2017-09-29 13:32:27
【问题描述】:

这是我的 MySQL 存储过程。

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

这是我的 ASP 代码。

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

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

帮我提建议。

【问题讨论】:

    标签: c# mysql asp.net .net stored-procedures


    【解决方案1】:

    你可以在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();
        }
    

    【讨论】:

      【解决方案2】:

      考虑像这样使用MySqlParameter 列表:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-13
        • 2021-11-24
        • 1970-01-01
        • 2011-05-05
        • 1970-01-01
        相关资源
        最近更新 更多