存储过程返回意外值但工作正常

时间:2014-04-02 06:28:18

标签: asp.net sql c#-4.0 ado.net c#-3.0

我的项目工作得很好,但很奇怪。我写了存储过程,如果记录已经不存在则插入记录然后它插入并返回1否则返回0 在asp.net .cs代码我输入一个条件,如果结果返回1然后返回true否则返回false并且它完成但完全但它实际上SP没有返回1,它在两个条件下都返回0但程序工作正常对于1和0条件,为什么?我不明白。

SP:

ALTER PROCEDURE [dbo].[AddCoordinates]
    -- Add the parameters for the stored procedure here

    @AddedDateTime varchar(50),
    @IMEI varchar(50),
    @RecordedDateTime varchar(50),
    @Latitude varchar(50),
    @Longitude varchar(50),
    @IsParking bit,
    @result int output
AS
BEGIN

    --set @result = (select count(*) from dbo.Coordinates where IMEI =@IMEI)

    If Not exists(select IMEI from dbo.Coordinates where IMEI =@IMEI)
    Begin
     insert into dbo.Coordinates 
     values (@AddedDateTime, @IMEI, @RecordedDateTime, @Latitude, @Longitude, @IsParking) 
     Select @result=1
    End
    Else If exists(select IMEI from dbo.Coordinates where IMEI =@IMEI)
    Begin
     Select @result=0
    End

END

.BLL

public class BLL
{
    String conStr;
    String query;
    SqlConnection sqlCon;
    SqlCommand sqlCom;

    public BLL()
    {

        conStr = WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        query  = "";
        sqlCon = new SqlConnection(conStr);
        sqlCom = new SqlCommand(query, sqlCon);

    }

    public bool InsertCoordinates(string[] parts) 
    {

        sqlCom.CommandText = "AddCoordinates";
        sqlCom.CommandType = CommandType.StoredProcedure;
        int result;

        sqlCom.Parameters.Add("@AddedDateTime", SqlDbType.VarChar).Value = parts[0].ToString();
        sqlCom.Parameters.Add("@IMEI", SqlDbType.VarChar).Value = parts[1].ToString();
        sqlCom.Parameters.Add("@RecordedDateTime", SqlDbType.VarChar).Value = parts[2].ToString();
        sqlCom.Parameters.Add("@Latitude", SqlDbType.VarChar).Value = parts[3].ToString(); ;
        sqlCom.Parameters.Add("@Longitude", SqlDbType.VarChar).Value = parts[4].ToString(); ;
        sqlCom.Parameters.Add("@IsParking ", SqlDbType.Bit).Value = ((parts[5].ToString().Trim()).Equals("0")) ? true : false;
        SqlParameter sqlParam = new SqlParameter("@result", SqlDbType.Int);
        sqlCom.Parameters.Add(sqlParam);
        sqlCom.Parameters["@result"].Direction = ParameterDirection.Output;

        try
        {
            sqlCon.Open();
            result = Convert.ToInt32(sqlCom.ExecuteNonQuery());


        }
        catch (Exception ex)
        {
            throw ex;

        }
        finally 
        {
            sqlCon.Close();
        }

        if(result == 1)
           return true;
        else
           return false;
    }
}

.Webservice

public class GPSWebservice : System.Web.Services.WebService {

    public GPSWebservice () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public bool insertCoordinates(string[] parts) 
    {
        BLL bll = new BLL();
        return bll.InsertCoordinates(parts);
    }

}

.default.cs

protected void btnInsertCoordinates_Click(object sender, EventArgs e)
    {
        GPSWebservice gpsws = new GPSWebservice();
        string[] parts = "2013-03-31,ALFALAH-8000,2013-03-31,1099,9888, 0".Split(',');
        Response.Write( gpsws.insertCoordinates(parts));
    }

我在结果= convert.toint32(sqlCom.executenonquery())中设置了断点,我尝试了ExecuteScalar()但同样的问题。

1 个答案:

答案 0 :(得分:1)

问题在这里

result = Convert.ToInt32(sqlCom.ExecuteNonQuery());

必须是

sqlCom.ExecuteNonQuery();
result = sqlCom.Parameters["@result"].Value;

或者,在您的情况下

result = sqlParam.Value;

输出参数不是返回值。

您可以将上述代码更改为

SqlParameter sqlParam = new SqlParameter("@result", SqlDbType.Int);
sqlParam.Direction = ParameterDirection.Output;
sqlCom.Parameters.Add(sqlParam);

更具可读性。