21

I have a SQL function that returns an INT, when I try to call it via dapper I always get no results back.

I'm calling it like this:

var result = _connection.Query<int>("functionname", new {Parm = 123}, commandType: CommandType.StoredProcedure);

Does dapper support sql functions?

2 Answers 2

16

Dapper should support it. Are you sure, your function is in the right database?

Here's a quick VB.NET example.

   Using conn = IDbConnectionFactory.CreateFromProvider("System.Data.SqlClient", CONNECTION_STRING)
                Dim sqlCommand As String = "SELECT dbo.fx_SumTwoValues(@valueOne,@valueTwo) As SumOfTwoValues"
                conn.Open()
                Dim result = (conn.Query(Of Integer)(sqlCommand, New With {.valueOne = 1, .valueTwo = 2})).First()
                Console.WriteLine(result.ToString)
            End Using

And here's the function I created on the same db I'm using in my connection.

CREATE FUNCTION fx_SumTwoValues
( @Val1 int, @Val2 int )
RETURNS int
AS
BEGIN
  RETURN (@Val1+@Val2)
END
GO
Sign up to request clarification or add additional context in comments.

2 Comments

The bit I was doing wrong was calling functionname and it should have been select dbo.functionname as columnname
@hellboy Any exception thrown by the ODP.NET (Or whatever you use) driver is propagated to the caller.
9

Can use this:

result = _connection.Query<dynamic>("SELECT dbo.functionName(@Parm)", new {Parm = 123},commandType: CommandType.Text);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.