C# 过程的参数数不正确。。。来自C
在存储过程中使用参数时遇到问题。我相信我没有通过C正确地传递参数 我的代码大致相当于:C# 过程的参数数不正确。。。来自C,c#,mysql,sql,stored-procedures,ado.net,C#,Mysql,Sql,Stored Procedures,Ado.net,在存储过程中使用参数时遇到问题。我相信我没有通过C正确地传递参数 我的代码大致相当于: public static string GetCustomer(string storedProcedure, string connectionString) { DataTable dt = new DataTable(); using (MySqlConnection con = new MySqlConnection(connectionString)) {
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 "";
}
程序tspos.get_customer的参数数量不正确;预期为2,得到0
下面的调用按预期工作,因此我认为我的问题在C语言中
CALL get_customer('table1', 'table2');
CALL get_customer('table3', 'table4');
我认为您的参数名称应以@符号作为前缀: 此外,请确保适当设置命令类型:
using (MySqlConnection con = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand(storedProcedure, con))
{
//Set command type
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@_table1", "table1");
cmd.Parameters.AddWithValue("@_table2", "table2");
IDataParameter[] temp = da.GetFillParameters();//returns 2 parameters
da.Fill(dt);
//Irrelevant code
}
}
我认为您的参数名称应以@符号作为前缀: 此外,请确保适当设置命令类型:
using (MySqlConnection con = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand(storedProcedure, con))
{
//Set command type
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@_table1", "table1");
cmd.Parameters.AddWithValue("@_table2", "table2");
IDataParameter[] temp = da.GetFillParameters();//returns 2 parameters
da.Fill(dt);
//Irrelevant code
}
}
由于您已经使用了MySqlDataAdapter,因此最好使用指定参数,也不要忘记,您需要在参数名称之前添加一个@,如下所示:
da.SelectCommand.Parameters.AddWithValue("@_table1", "table1");
cmd.CommandType = CommandType.StoredProcedure;
尽管直接指定类型并使用Value属性比AddWithValue更好
另一方面,您需要使用以下代码指定要作为存储过程的命令类型:
da.SelectCommand.Parameters.AddWithValue("@_table1", "table1");
cmd.CommandType = CommandType.StoredProcedure;
由于您已经使用了MySqlDataAdapter,因此最好使用指定参数,也不要忘记,您需要在参数名称之前添加一个@,如下所示:
da.SelectCommand.Parameters.AddWithValue("@_table1", "table1");
cmd.CommandType = CommandType.StoredProcedure;
尽管直接指定类型并使用Value属性比AddWithValue更好
另一方面,您需要使用以下代码指定要作为存储过程的命令类型:
da.SelectCommand.Parameters.AddWithValue("@_table1", "table1");
cmd.CommandType = CommandType.StoredProcedure;
不幸的是,这并没有解决问题。相同的错误。@EricWarburton:da.GetFillParameters方法做什么?它在DbDataAdapter中说:Summary:获取用户在执行SQL SELECT语句时设置的参数。我用它只是为了检查我的参数是否设置在da中。@EricWarburton:对不起,我误读了适配器da的名称。我认为这是这个方法范围之外的其他类。我想知道调用它是否会删除您的参数。。。我更新了我的答案,所以试试看会发生什么。@EricWarburton:我现在几乎100%确定您的问题是@符号以及未设置的命令类型。请检查我的答案。不幸的是,这并没有解决问题。相同的错误。@EricWarburton:da.GetFillParameters方法做什么?它在DbDataAdapter中说:Summary:获取用户在执行SQL SELECT语句时设置的参数。我用它只是为了检查我的参数是否设置在da中。@EricWarburton:对不起,我误读了适配器da的名称。我认为这是这个方法范围之外的其他类。我想知道调用它是否会删除您的参数。。。我更新了我的答案,所以试试看会发生什么。@EricWarburton:我现在几乎100%确定您的问题是@符号以及未设置的命令类型。请核对我的答案。