C# 即使提供了参数,SQL Server存储过程也需要参数
我正在使用MS Visual Studio 2010和SQL Server 2008 R2 在C#应用程序中,我试图使用存储过程,但遇到了一个奇怪的错误 以下是表格定义:C# 即使提供了参数,SQL Server存储过程也需要参数,c#,stored-procedures,sql-server-2008-r2,C#,Stored Procedures,Sql Server 2008 R2,我正在使用MS Visual Studio 2010和SQL Server 2008 R2 在C#应用程序中,我试图使用存储过程,但遇到了一个奇怪的错误 以下是表格定义: create table bed_allotment ( bill_id bigint, bed_category varchar(50), for_days int ) 存储过程: create procedure AddBed ( @bill_id bigint, @bed_cat
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'
怎么了?任何建议都会大有帮助。谢谢大家! 将更改为,使请求成为。将更改为,使请求成为。尝试此操作
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;
试试这个
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;
哦,伙计,我在想我怎么会忘记设置命令类型…谢谢哦,伙计,我在想我怎么会忘记设置命令类型…谢谢