即使提供了参数,SQL Server存储过程也需要参数

时间:2013-08-23 05:27:45

标签: c# stored-procedures sql-server-2008-r2

我正在使用MS Visual Studio 2010和SQL Server 2008 R2。

在C#app中我试图使用存储过程,但遇到了一个奇怪的错误。

这是表格定义:

create table bed_allotment
(
    bill_id bigint,
    bed_category varchar(50),
    for_days int
)

存储过程:

create procedure AddBed
(
    @bill_id bigint,
    @bed_category varchar(50),
    @for_days int
)
as
begin
    insert into bed_allotment 
    values (@bill_id, @bed_category, @for_days)
end

按钮上的代码点击:

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conString);
    SqlCommand AddBedCommand = new SqlCommand("AddBed", con);

    AddBedCommand.Parameters.AddWithValue("@bill_id", 1330);
    AddBedCommand.Parameters.AddWithValue("@bed_category", "ICCU");
    AddBedCommand.Parameters.AddWithValue("@for_days", 6);

    con.Open();
    AddBedCommand.ExecuteNonQuery();
    con.Close();
}

执行此操作时,会发生错误。它说

  

SQL过程需要参数'@bill_id'未提供

错误是什么?任何建议都将是一个很大的帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

试试这个

AddBedCommand.CommandType = CommandType.StoredProcedure;
AddBedCommand.Parameters.Add("@bill_id", SqlDbType.Int).Value = 1330;
AddBedCommand.Parameters.Add("@bed_category", SqlDbType.VarChar).Value = "ICCU";
AddBedCommand.Parameters.Add("@for_days", SqlDbType.Int).Value = 6;