在sql中存储的Proc不返回值

Ram*_*Ram 2 c# sql sql-server asp.net ado.net

我的函数没有返回任何东西 - strReturn为空:

        try
        {
            SqlParameter[] parameter = new SqlParameter[]
            {
                new SqlParameter("@MerchantID", MercahntID),
                new SqlParameter("@LoactionID", LoactionID)
            };

            SqlHelper.ExecuteNonQuery(DbConnString, System.Data.CommandType.StoredProcedure, "GetMerchantLocationZip", parameter);

            return strReturn;
        }
        catch (Exception ex)
        {
            LogError("Error Occurred When Retrieving Mercahnt Location Zip: MercahntID:" + MercahntID.ToString(), ex);
            return strReturn;
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我使用'exec GetMerchantLocationZip(3333,373773)执行此存储过程时,我在SQL中获得了正确的zipcode.为什么我不在Visual Studio中获取它?

Create PROCEDURE [dbo].[GetMerchantLocationZip](
@MerchantID bigint,
@LoactionID bigint)

AS 

Begin

Select Zip FROM Merchant_Location 
where MerchantID=@MerchantID AND LocationID =@LoactionID

End
Run Code Online (Sandbox Code Playgroud)

我正在学习,如果这是一个明显的错误,请道歉.谢谢大家!

Jus*_*ner 6

您没有获得结果,因为您没有将代码作为查询执行.

你正在打电话SqlHelper.ExecuteNonQuery(),不会返回任何结果.

看起来你正在使用SqlHelper应用程序块,所以我认为你想要的代码是(如果你在查询中返回多行):

DataSet ds = SqlHelper.ExecuteDataSet(DbConnString,
                                      CommandType.StoredProcedure,
                                      "GetMerchantLocationZip",
                                      parameter);
Run Code Online (Sandbox Code Playgroud)

ds 然后将包含查询的结果.

如果您尝试从数据库而不是一组行中检索单个值,那么您的代码将是:

object zip = SqlHelper.ExecuteScalar(DbConnString,
                                     CommandType.StoredProcedure,
                                     "GetMerchantLocationZip",
                                     parameter);
Run Code Online (Sandbox Code Playgroud)