存储过程或函数需要但未提供该参数

问题描述:

我想通过调用存储过程来将数据插入到SQL Server数据库中,但我得到的错误

I am trying to insert data into a SQL Server database by calling a stored procedure, but I am getting the error

*过程或函数'插入'需要参数'@Emp_no',但未提供该*

*Procedure or function 'Insertion' expects parameter '@Emp_no', which was not supplied*

我的存储过程被称为插入。我已经彻底检查,并没有任何参数也不翼而飞我已使用标签检查它。标签显示的价值,但我不知道为什么我收到错误。

My stored procedure is called Insertion. I have checked it thoroughly and no parameters is missing also I have checked it by using a label. The label shows the value but I don't know why I am getting the error.

我的code是

    try
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Parameters.Clear();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Insertion";
        cmd.Connection = con;

        if (rdb_Male.Checked)
        {
            int @Emp_no = Convert.ToInt32(txtbx_Empno.Text);
            string @Emp_name = txtbx_Emp_Name.Text;
            double @phone = Convert.ToDouble(txtbx_Phone.Text);
            string @Email = txtbx_Email.Text;
            string @Password = txtbx_Pwd.Text;
            string @Gender = rdb_Male.Text;
            DateTime @Dob = Convert.ToDateTime(dob);
            string @Address = txtbx_Address.Text;
            string @Designation = txtbx_Designation.Text;
            string @Qualification = txtbx_Qual.Text;
            double @Experience = Convert.ToDouble(txtbx_Exp.Text);
            double @Salary = Convert.ToDouble(txtbx_Sal.Text);
            DateTime @Doj = Convert.ToDateTime(doj);
        }
        else if (rdb_Female.Checked)
        {
            int @Emp_no = Convert.ToInt32(txtbx_Empno.Text);
            string @Emp_name = txtbx_Emp_Name.Text;
            double @phone = Convert.ToDouble(txtbx_Phone.Text);
            string @Email = txtbx_Email.Text;
            string @Password = txtbx_Pwd.Text;
            string @Gender = rdb_Female.Text;
            DateTime @Dob = Convert.ToDateTime(dob);
            string @Address = txtbx_Address.Text;
            string @Designation = txtbx_Designation.Text;
            string @Qualification = txtbx_Qual.Text;
            double @Experience = Convert.ToDouble(txtbx_Exp.Text);
            double @Salary = Convert.ToDouble(txtbx_Sal.Text);
            DateTime @Doj = Convert.ToDateTime(doj);
        }

        if (con.State==ConnectionState.Closed)
            con.Open();

        LABEL.Text = txtbx_Empno.Text;

        cmd.ExecuteNonQuery();

        lbl_Errormsg.Visible = true;
        lbl_Errormsg.Text = "Record Inserted Successfully";

        con.Close();
    }

和存储过程是

ALTER PROCEDURE dbo.Insertion
(
@Emp_no int,
@Emp_name varchar(30),
@phone numeric(10,0),
@Email varchar(30),
@Password varchar(10),
@Gender varchar(6),
@Dob date,
@Address varchar(100),
@Designation varchar(20),
@Qualification varchar(20),
@Experience numeric(4,2),
@Salary numeric(10,2),
@Doj date
)
AS
 Begin
   Insert into Register (Emp_no, Emp_name, phone, Email, Password, Gender, Dob, Address, Designation, Qualification, Experience, Salary, Doj)
   Values(@Emp_no, @Emp_name, @phone, @Email, @Password, @Gender, @Dob, @Address, @Designation, @Qualification, @Experience, @Salary, @Doj)
 End

请帮助我。先谢谢了。

您需要使用 SqlCommand.Parameters.AddWithValue

cmd.Parameters.AddWithValue("@ParameterName", value);

SqlCommand.Parameters.Add 用于其它数据类型:

or SqlCommand.Parameters.Add for other data types:

cmd.Parameters.Add("@ParameterName", SqlDbType.Int, 5);
cmd.Parameters["@ParameterName"].Value = value;

SqlCommand.Parameters.AddWithValue 替换的暧昧超载添加是使用一个字符串和对象参数。请参阅MSDN获取更多信息。

SqlCommand.Parameters.AddWithValue replaces the ambiguous overload of Add that took a string and object parameter. See MSDN for more info.