C#在将参数作为输出传递时出现问题

时间:2015-02-21 05:41:59

标签: c# parameters

我仍然收到错误"正式参数" @ result"未被声明为OUTPUT参数,而是在请求的输出中传递的实际参数" 。 这是我在c#中的代码

            SqlCommand cmd = new SqlCommand("AddRoomType", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@TypeName", TxtType.Text);
            cmd.Parameters.Add("@result", SqlDbType.Int);
            cmd.Parameters["@result"].Direction = ParameterDirection.Output;              


            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

我在SQL server中的存储过程

Create proce AddRoomType
@TypeName nvarchar(50),
@result int

as
if(exists(select * from TblRoomTypes where RoomType = @TypeName))
set @result = 0
else 
begin
set @result = 1
insert into TblRoomTypes (RoomType) values (@TypeName)
end

请帮助

1 个答案:

答案 0 :(得分:0)

在存储过程中将参数设置为OUTPUT

Create proce AddRoomType
@TypeName nvarchar(50),
@result int out

as
if(exists(select * from TblRoomTypes where RoomType = @TypeName))
set @result = 0
else 
begin
set @result = 1
insert into TblRoomTypes (RoomType) values (@TypeName)
end