How do I get a scalar value from a stored proc using Nettiers
-
04-10-2019 - |
Question
I have a really simple stored procedure that looks like this:
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
I'm trying to get nettiers to generate a method with this signature:
public long VisitorService.GetVisitorIDByVisitorGUID(GUID visitorGUID);
Basically I want Nettiers to call ExecuteScalar instead of ExecuteReader. What am I doing wrong?
Solution
Why not use the custom data access functionality to call your stored proc with ExecuteScalar? (http://nettiers.com/DataLayer.ashx#Read_Methods:_4)
var vistorId = (long)DataRepository.Provider.ExecuteScalar(CommandType.StoredProcedure, "_Visitor_GetVisitorIDByVisitorGUID");
The body of the stored proc should look like:
Select VisitorID
FROM dbo.Visitor
WHERE VisitorGUID = @VisitorGUID
Return
or
Declare @VisitorId bigint
Set @VisitorId = (
Select VisitorId
From dbo.Visitor
Where VisitorGuid = @VisitorGUID
)
Select @VisitorId
Return
OTHER TIPS
For those wanting to do this programatically, the above example will not work as there is no way to pass the necessary parameter value using any of that method's overrides.
var vistorId = (long)DataRepository.Provider.ExecuteScalar(CommandType.StoredProcedure, "_Visitor_GetVisitorIDByVisitorGUID");
The only way I've found to accomplish this is by passing a DbCommand as a lone argument to the ExecuteScalar method:
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);
}