【发布时间】:2021-01-15 11:54:14
【问题描述】:
我在存储过程中使用参数时遇到问题。我相信我没有使用 C# 正确传递参数。
我的代码大致相当于:
public static string GetCustomer(string storedProcedure, string connectionString)
{
DataTable dt = new DataTable();
using (MySqlConnection con = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand(storedProcedure, con))
{
cmd.Parameters.AddWithValue("_table1", "table1");
cmd.Parameters.AddWithValue("_table2", "table2");
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
IDataParameter[] temp = da.GetFillParameters();//returns 2 parameters
da.Fill(dt);//Breaks here with the error below
//Irrelevant code
}
}
return "";
}
PROCEDURE tspos.get_customer 的参数数量不正确;预期 2,得到 0
DROP PROCEDURE IF EXISTS get_customer;
DELIMITER //
CREATE PROCEDURE get_customer
(
IN _table1 VARCHAR(25),
IN _table2 VARCHAR(25)
)
BEGIN
SET @t1 = CONCAT('SELECT a.*, b.* FROM ', _table1, ' a, ', _table2, ' b');
PREPARE statement FROM @t1;
EXECUTE statement;
DEALLOCATE PREPARE statement;
END //
DELIMITER ;
下面的调用按预期工作,所以我想我的问题出在 C# 中
CALL get_customer('table1', 'table2');
CALL get_customer('table3', 'table4');
【问题讨论】:
标签: c# mysql sql stored-procedures ado.net