即使提供了参数,SQL Server 存储过程也需要参数 [英] SQL Server stored procedure expects parameter even though the parameter is provided

查看:39
本文介绍了即使提供了参数,SQL Server 存储过程也需要参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 MS Visual Studio 2010 和 SQL Server 2008 R2.

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

在 C# 应用程序中,我尝试使用存储过程,但遇到了一个奇怪的错误.

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

这里是表定义:

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

当我执行此操作时,发生错误.它说

When I execute this, error occurs. It says

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

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

怎么了?任何建议都会有很大帮助.谢谢!

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

推荐答案

更改 AddBedCommand.CommandTypeCommandType.StoredProcedure 以便请求是 RPC 请求.

这篇关于即使提供了参数,SQL Server 存储过程也需要参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆