我在c#中有一个功能是插入数据。它应该做的就是调用从我通过Fiddler提供的Request Body中给出的存储过程。现在我试图测试它,但我一直得到这个错误。SQLCommand在C#不提供参数
“过程或函数'spCreatePerson'需要参数'@first',它没有提供。”
这里是对象 -
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public int PersonTypeId { get; set; }
}
这里是请求主体 -
{"FirstName":"Test","LastName":"MuhFuh","Phone":"5555555555","Email":"[email protected]","PersonTypeId":1}
下面是函数调用的PROC-
public static int InsertData(string procName, Person p)
{
int rowsAffected = 0;
con = CreateConnection();
using (con)
{
cmd.CommandType = CommandType.StoredProcedure;
cmd = new SqlCommand("spCreatePerson", con);
cmd.Parameters.AddWithValue("@first", p.FirstName);
cmd.Parameters.AddWithValue("@last", p.LastName);
cmd.Parameters.AddWithValue("@email", p.Email);
cmd.Parameters.AddWithValue("@phone", p.Phone);
cmd.Parameters.AddWithValue("@pTypeID", p.PersonTypeId);
rowsAffected = cmd.ExecuteNonQuery();
}
return rowsAffected;
}
正如你可以看到我传入参数,但得到这个错误。有没有我在代码中错过的东西?我通过它,数据似乎传递得很好。
编辑
这里是存储程序 -
CREATE PROC [dbo].[spCreatePerson] @first nvarchar(100), @last
nvarchar(100), @email nvarchar(50), @phone nvarchar(100), @pTypeID int
AS
INSERT INTO Person(FirstName, LastName, Email, Phone, PersonTypeID)
VALUES(@first, @last, @email, @phone, @pTypeID)
GO
调试时,不'p.FirstName'包含值? – David
@David是的。正在从请求主体传入“测试”。我调试了所有的数据似乎正在经历。 –
你可以在这里添加存储的proc脚本到这个问题吗? – Sujith