使用 C# 调用存储过程
private void btnGo_Click(object sender, EventArgs e)
{
Array IDlist = txtUserID.Text.Split(new char[] { });
ArrayList badID = new ArrayList();
foreach (string textLine in IDlist)
{
try
{
int LineID = Convert.ToInt32(textLine);
string emp = txtDistricts.Text;
command.Parameters.Add("@EmpID", SqlDbType.Int).Value = LineID;
if (!emp.Equals(string.Empty))
command.Parameters.Add("@SchoolDistricts", SqlDbType.NVarChar).Value = emp;
else command.Parameters.Add("@SchoolDistricts", SqlDbType.NVarChar).Value = DBNull.Value;
if (cbRemove.Checked)
command.Parameters.Add("@Options", SqlDbType.Int).Value = 1;
else if (cbReset.Checked)
command.Parameters.Add("@Options", SqlDbType.Int).Value = 0;
else command.Parameters.Add("@Options", SqlDbType.Int).Value = DBNull.Value;
SqlParameter returnValue = new SqlParameter("@return_value", DbType.String);
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
conn.Open();
command.Connection = conn;
// command.ExecuteNonQuery();
command.ExecuteScalar();
String OutPutCheck = (command.Parameters["@return_value"].Value.ToString());
String getCheck = (command.ExecuteScalar().ToString());
OPBox.Text += LineID + "--->>" + OutPutCheck + "--->>" + getCheck + "\n";
conn.Close();
//flagUser(LineID, emp);
}
catch (Exception ex)
{
//stored procedure error
badID.Add(textLine);
conn.Close();
}
}}
我做了一个APP,它一次需要一堆ID。 btn_click 之后,这些值放入数组中。然后从数组中将每个ID一一传递给存储过程,并得到返回值。第一个值给出返回值,但是之后当第二个值传递给存储过程时,它会给出以下错误。
> ERROR::::ex = {"Procedure or function
> usp_Flag_Employee has too many
> arguments specified."}
private void btnGo_Click(object sender, EventArgs e)
{
Array IDlist = txtUserID.Text.Split(new char[] { });
ArrayList badID = new ArrayList();
foreach (string textLine in IDlist)
{
try
{
int LineID = Convert.ToInt32(textLine);
string emp = txtDistricts.Text;
command.Parameters.Add("@EmpID", SqlDbType.Int).Value = LineID;
if (!emp.Equals(string.Empty))
command.Parameters.Add("@SchoolDistricts", SqlDbType.NVarChar).Value = emp;
else command.Parameters.Add("@SchoolDistricts", SqlDbType.NVarChar).Value = DBNull.Value;
if (cbRemove.Checked)
command.Parameters.Add("@Options", SqlDbType.Int).Value = 1;
else if (cbReset.Checked)
command.Parameters.Add("@Options", SqlDbType.Int).Value = 0;
else command.Parameters.Add("@Options", SqlDbType.Int).Value = DBNull.Value;
SqlParameter returnValue = new SqlParameter("@return_value", DbType.String);
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
conn.Open();
command.Connection = conn;
// command.ExecuteNonQuery();
command.ExecuteScalar();
String OutPutCheck = (command.Parameters["@return_value"].Value.ToString());
String getCheck = (command.ExecuteScalar().ToString());
OPBox.Text += LineID + "--->>" + OutPutCheck + "--->>" + getCheck + "\n";
conn.Close();
//flagUser(LineID, emp);
}
catch (Exception ex)
{
//stored procedure error
badID.Add(textLine);
conn.Close();
}
}}
I made an APP , which takes bunch of ID at a time. After btn_click these values put in array. Then from array each ID pass to store procedure one by one, and get return value. well First value give return value, but after that when second value pass to store procedure it gives following error.
> ERROR::::ex = {"Procedure or function
> usp_Flag_Employee has too many
> arguments specified."}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不断向命令对象添加参数而不重置它。您应该将连接和命令对象移动到调用它们的方法中并使用“using”语句。
因为您的连接和命令是类字段,所以循环的每个实例都会将参数重新添加到旧的参数集中。至少,在循环顶部重置参数集合。
You keep adding parameters to your command object without reseting it. You should move your connection and command objects into the method where they are being called and use 'using' statements.
Because your connection and command are class fields, each instance of the loop is re-adding the parameters to the old set of parameters. At minimum, reset the parameters collection at the top of the loop.
您向该过程传递了太多参数。如果您粘贴过程代码,我们可以帮助识别,但是只需计算参数并检查以确保您已在过程中定义所有参数。
You are passing too many parameters to the procedure. If you paste the procedure code we can help identify, however just do a count of the params and check to ensure you have all defined in the proc.
我在您的示例中没有看到任何生成
SqlCommand
对象的代码。如果命令是类的本地命令,则很有可能它已经被使用过(这意味着它可能已经添加了参数)。
我还没有看到任何将命令类型设置为 StoredProcedure 的代码。根据命令文本的内容,这也可能是问题(如果您只是传递存储过程名称而不设置类型......它将看到该命令没有参数)。
重新编写代码以使用自己的
SqlConnection
和SqlCommand
将使调试变得更加容易(当然,除非它已经是并且您没有给我们这个信息)代码)。编辑
我刚刚注意到您在 foreach 循环内使用代码而没有清除参数。这是另一个问题(并且可能是此问题最可能的原因)。只需确保在添加新参数之前在每个循环开始时调用
command.Parameters.Clear()
即可。I don't see any code generating the
SqlCommand
object in your example.If command is local to the class, there's a very good chance that it has already been used (which means it probably already has parameters added to it).
I also see no code that sets the command type to
StoredProcedure
. Depending on what the command text is, this could be the issue as well (if you're simply passing the stored procedure name without setting the type...it will see the command as having no parameters).Re-Writing the code to use its own
SqlConnection
andSqlCommand
would make this much easier to debug (unless, of course, it already is and you didn't give us that code).EDIT
I just noticed that you're using the code inside a foreach loop without clearing the parameters. That is yet another issue (and probably the most likely cause for this issue). Just be sure to call
command.Parameters.Clear()
at the beginning of each loop before adding the new parameters.调用清除 方法之前添加参数。
Call the Clear method before adding the parameters.
它第一次起作用是因为您的命令对象没有参数。对于后续的每次迭代,您都会继续向
command
对象添加另一组参数。您需要 在每次迭代时清除
command
对象的参数。It works the first time because your
command
object has no parameters. For each subsequent iteration you keep on adding another set of parameters to yourcommand
object.You need to clear the parameters for your
command
object on each iteration.