我想在MVC中使用存储过程,但在尝试使用id:
更新数据时出错了过程或函数'StpEmployee'需要参数'@EloyloyID',这是未提供的
这是我的存储过程:
CREATE PROCEDURE StpEmployee
(
@EmployeeID INT,
@EmpLastName NVARCHAR(20),
@EmpFirstName NVARCHAR(50),
@EmpAge INT,
@EmpAddress NVARCHAR(255)= NULL,
@EmpSalary DECIMAL = NULL,
@EmpEmail NVARCHAR(100) = NULL,
@EmpPhone NVARCHAR(10) = NULL,
@TranType nvarchar(20) = ''
)
AS
BEGIN
IF @TranType = 'Insert'
BEGIN
INSERT INTO Employee(EmpLastName,EmpFirstName,EmpAge,EmpAddress,EmpSalary,EmpEmail,EmpPhone)VALUES(@EmpLastName,@EmpFirstName,@EmpAge,@EmpAddress,@EmpSalary,@EmpEmail,@EmpPhone)
END
IF @TranType = 'Select'
BEGIN
SELECT * FROM Employee
END
IF @TranType = 'Update'
BEGIN
UPDATE Employee SET EmpLastName=@EmpLastName,EmpFirstName=@EmpFirstName,EmpAge=@EmpAge,EmpAddress=@EmpAddress,EmpSalary=@EmpSalary,EmpEmail=@EmpEmail,EmpPhone=@EmpPhone
WHERE EmployeeID=@EmployeeID
END
ELSE IF @TranType = 'Delete'
BEGIN
DELETE FROM Employee WHERE EmployeeID=@EmployeeID
END
ELSE IF @TranType = 'Search'
BEGIN
SELECT * FROM Employee WHERE EmpLastName LIKE @EmpLastName OR EmpFirstName LIKE @EmpFirstName
OR EmpAddress LIKE @EmpAddress OR EmpEmail LIKE @EmpEmail OR EmpPhone LIKE @EmpPhone
END
结束
这是我的控制器:
using (con)
{
using (cmd = con.CreateCommand())
{
cmd.CommandText = "StpEmployee";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
try
{
cmd.Parameters.AddWithValue("@TranType", "Update");
cmd.Parameters.AddWithValue("@empID", EmployeeID);
cmd.Parameters.AddWithValue("@lastname", lname);
cmd.Parameters.AddWithValue("@firstname", fname);
cmd.Parameters.AddWithValue("@age", age);
cmd.Parameters.AddWithValue("@address", add);
cmd.Parameters.AddWithValue("@salary", salary);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@phone", phone);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
return View("Error", new HandleErrorInfo(ex, "Employee", "Edit"));
}
}
}
答案 0 :(得分:1)
更改此行:
cmd.Parameters.AddWithValue("@empID", EmployeeID);
对此:
cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
其他参数的所有都会出现同样的问题。所以改变它们。
cmd.Parameters.AddWithValue("@EmpLastName", lname);
cmd.Parameters.AddWithValue("@EmpFirstName", fname);
cmd.Parameters.AddWithValue("@EmpAge", age);
cmd.Parameters.AddWithValue("@EmpAddress", add);
cmd.Parameters.AddWithValue("@EmpSalary", salary);
cmd.Parameters.AddWithValue("@EmpEmail", email);
cmd.Parameters.AddWithValue("@EmpPhone", phone);
参数的名称必须在存储过程和代码中匹配。