我有一个非常简单的存储过程,如下所示:
CREATE PROCEDURE _Visitor_GetVisitorIDByVisitorGUID
(
@VisitorGUID AS UNIQUEIDENTIFIER
)
AS
DECLARE @VisitorID AS bigint
SELECT @VisitorID = VisitorID FROM dbo.Visitor WHERE VisitorGUID = @VisitorGUID
--Here's what I've tried
RETURN @VisitorID 'Returns an IDataReader
SELECT @VisitorID 'Returns an IDataReader
--I've also set it up with a single output
--parameter, but that means I need to pass
--the long in by ref and that's hideous to me
我正在尝试让nettiers生成一个带有此签名的方法:
public long VisitorService.GetVisitorIDByVisitorGUID(GUID visitorGUID);
基本上我希望Nettiers调用ExecuteScalar而不是ExecuteReader。我做错了什么?
答案 0 :(得分:2)
为什么不使用自定义数据访问功能来使用ExecuteScalar调用存储过程? (http://nettiers.com/DataLayer.ashx#Read_Methods:_4)
var vistorId = (long)DataRepository.Provider.ExecuteScalar(CommandType.StoredProcedure, "_Visitor_GetVisitorIDByVisitorGUID");
存储过程的主体应如下所示:
Select VisitorID
FROM dbo.Visitor
WHERE VisitorGUID = @VisitorGUID
Return
或
Declare @VisitorId bigint
Set @VisitorId = (
Select VisitorId
From dbo.Visitor
Where VisitorGuid = @VisitorGUID
)
Select @VisitorId
Return
答案 1 :(得分:0)
对于那些想以编程方式执行此操作的人,上面的示例将无法工作,因为无法使用该方法的任何覆盖来传递必要的参数值。
var vistorId = (long)DataRepository.Provider.ExecuteScalar(CommandType.StoredProcedure, "_Visitor_GetVisitorIDByVisitorGUID");
我发现完成此任务的唯一方法是将DbCommand作为一个单独的参数传递给ExecuteScalar方法:
int myReturnID = 0;
using (SqlCommand cmd = new SqlCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_MyStoredProcedure";
cmd.Parameters.Add(new SqlParameter("MyParameter", myParameter));
myReturnID = (Int32)DataRepository.Provider.ExecuteScalar(cmd);
}