SQL Server stored procedure expects parameter even though the parameter is provided

吃可爱长大的小学妹 提交于 2019-12-24 14:26:14

问题


I am using MS Visual Studio 2010 and SQL Server 2008 R2.

In C# app I am trying to use a stored procedure but came accross a strange error.

Here is table definition:

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

Stored procedure:

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

Code on button click:

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();
}

When I execute this, error occurs. It says

SQL procedure expects parameter '@bill_id' which was not provided

What's the mistake? Any suggestions will be a great help. Thank you!


回答1:


Change the AddBedCommand.CommandType to CommandType.StoredProcedure so that the request is an RPC request.




回答2:


Try this

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;


来源:https://stackoverflow.com/questions/18395369/sql-server-stored-procedure-expects-parameter-even-though-the-parameter-is-provi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!