由于@Parameter1 不是过程的参数而出现错误

Getting Error as @Parameter1 is not a parameter for procedure

我正在从 this 视频中学习 mvc,我在视频中从表单获取数据并将其保存到数据库。当我 运行 我的代码首先给出了一个错误说明

Procedure or function 'spAddEmployee' expects parameter '@Employee_Name', which was not supplied.

我遵循了一个解决方案,其中我将我的存储过程初始化为 NULL,但现在我收到另一个错误,指出“@Parameter1 不是过程的参数”。

这是我的存储过程

ALTER PROCEDURE [dbo].[spAddEmployee]
(
    @Employee_Name varchar(max) = NULL,
    @Employee_Age int = NULL,
    @Employee_Salary int = NULL,
    @Employee_City varchar(50) = NULL
)
AS
BEGIN
    INSERT INTO tblEmployee (Employee_Name, Employee_Age, Employee_Salary,  Employee_City)
    VALUES (@Employee_Name, @Employee_Age, @Employee_Salary, @Employee_City)
END

这是我的 ADO.NET 代码:

public void AddEmployee(Employee employee)
{
    string connectionString = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString;

    using (SqlConnection con = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand("spAddEmployee", con)
        {
            CommandType = CommandType.StoredProcedure
        };

        SqlParameter paramName = new SqlParameter();
        paramName.ParameterName = "@Employee_Name";
        paramName.Value = employee.Employee_Name;
        cmd.Parameters.Add(paramName);

        SqlParameter paramAge = new SqlParameter();
        paramAge.ParameterName = "@Employee_Age";
        paramAge.Value = employee.Employee_Age;
        cmd.Parameters.Add(paramAge);

        SqlParameter paramSalary = new SqlParameter();
        paramSalary.ParameterName = "@Employee_Salary";
        paramSalary.Value = employee.Employee_Salary;
        cmd.Parameters.Add(paramSalary);

        SqlParameter paramCity = new SqlParameter();
        paramCity.ParameterName = "@Employee_City";
        paramCity.Value = employee.Employee_City;
        cmd.Parameters.Add(paramCity);

        con.Open();
        cmd.ExecuteNonQuery();
    }
}

这是我的控制器代码

[HttpPost]
[ActionName("Create")]
public ActionResult Create_Post(FormCollection formCollection)
{
    Employee employee = new Employee();
    employee.Employee_Name = formCollection["Employee_Name"];
    employee.Employee_Age = Convert.ToInt32(formCollection["Employee_Age"]);
    employee.Employee_Salary = Convert.ToInt32(formCollection["Employee_Salary"]);
    employee.Employee_City = formCollection["Employee_City"];
    
    EmployeeBuissnessLayer employeeBuissnessLayer = new EmployeeBuissnessLayer();
    employeeBuissnessLayer.AddEmployee(employee);
    return RedirectToAction("Index");
}

我在第

行遇到错误
employeeBuissnessLayer.AddEmployee(employee);

请帮助我尝试了很多解决方案,但其中 none 个都有效。

这个

    SqlParameter paramName = new SqlParameter();
    paramName.ParameterName = "@Employee_Name";
    paramName.Value = employee.Employee_Name;
    cmd.Parameters.Add(paramName);

应该是

    SqlParameter paramName = new SqlParameter();
    paramName.ParameterName = "@Employee_Name";
    // Always set the datatype
    paramName.SqlDbType = SqlDbType.NVarChar;
    // For strings, always set the length
    paramName.Size = 128; // Max string length
    // Ensure you pass DBNull not C# null
    paramName.Value = employee.Employee_Name ?? System.DBNull.Value;
    cmd.Parameters.Add(paramName);

注意:最好不要使用 sp 前缀命名存储过程。