简体   繁体   中英

Calling a stored procedure using 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();
            }
        }}

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."}

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.

在添加参数之前,请调用Clear方法。

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 your command object.

You need to clear the parameters for your command object on each iteration.

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 and SqlCommand 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM