ASP.NET中的SQL存储过程问题

时间:2010-11-27 10:37:47

标签: c# asp.net sql windows-server-2008

我有像

这样的存储过程
CREATE procedure [dbo].[spAddItem]
(
  @itemId nvarchar(50),
  @itemName nvarchar(500),
  @itemDescription nvarchar(500),
  @itemImage nvarchar(100),
  @cityId  bigint,
  @Active int,
  @Status bigint output
)
as
begin tran
  declare @count as int
  declare @result as int  
  Set NOCOUNT ON

  select @count = Count(*) from Item where itemName = @itemName and itemId = @itemId

  if(@count > 0)
           begin
              set @result = -1;
           end
  else  
       begin 
                insert into Item(itemId,itemName,itemDescription,itemImage,cityId,Active)
                values (@itemId,@itemName,@itemDescription,@itemImage,@cityId,@Active) 
                set @result = 0  
        end

        if @@error=0  
          begin  
           commit tran 
           select @Status=@result
           return @Status  
          end  
         else  
          begin  
           rollback tran
           select @Status=@result
           return @Status     
          end

commit tran

&安培;在我的函数中获取状态参数我正在使用函数

public void DALInsertItem(ItemBLL it)
    {
        int rowNo;
        using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]))
        {
            try
            {
                SqlParameter[] par = new SqlParameter[7];

                par[0] = new SqlParameter("@itemId",it.itemId);
                par[1] = new SqlParameter("@itemName",it.itemName);
                par[2] = new SqlParameter("@itemDescription", it.itemDescription);
                par[3] = new SqlParameter("@itemImage", it.itemImage);
                par[4] = new SqlParameter("@cityId",it.cityId);
                par[5] = new SqlParameter("@Active", it.itemActive);
                par[6] = new SqlParameter("@Status", 100);
                par[6].Direction = ParameterDirection.Output;

                rowNo = SqlHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "spAddItem", par);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }

            if (rowNo == -1)
            {
                it.isExistItem = false;
            }
            else
            {
                it.isExistItem = true;
            }

        }
    }

问题是我没有在我的函数中获取状态(输出参数),

如何访问我的存储过程中设置的函数中的status参数。

由于

2 个答案:

答案 0 :(得分:5)

在调用存储过程后,您需要访问该输出参数:

using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]))
{
   try
   {
      SqlParameter[] par = new SqlParameter[7];

      par[0] = new SqlParameter("@itemId",it.itemId);
      par[1] = new SqlParameter("@itemName",it.itemName);
      par[2] = new SqlParameter("@itemDescription", it.itemDescription);
      par[3] = new SqlParameter("@itemImage", it.itemImage);
      par[4] = new SqlParameter("@cityId",it.cityId);
      par[5] = new SqlParameter("@Active", it.itemActive);
      par[6] = new SqlParameter("@Status", 100);
      par[6].Direction = ParameterDirection.Output;

      rowNo = SqlHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "spAddItem", par);

      int returnStatus = (int)par[6].Value;
  }

答案 1 :(得分:1)

您将从存储过程返回@@status,因此您可以使用ExecuteScalar代替ExecuteNonQuery来返回状态