2012-03-26 13 views
6

Ho una funzione SQL che restituisce un INT, quando provo a chiamarlo via dapper non ottengo mai risultati.Can Dapper può restituire valori da una funzione SQL?

sto chiamando in questo modo:

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

Does funzioni SQL supporto Dapper?

risposta

8

Dapper dovrebbe supportarlo. Sei sicuro, la tua funzione è nel database giusto?

Ecco un rapido esempio di VB.NET.

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 

Ed ecco la funzione che ho creato sullo stesso db che sto usando nella mia connessione.

CREATE FUNCTION fx_SumTwoValues 
(@Val1 int, @Val2 int) 
RETURNS int 
AS 
BEGIN 
    RETURN (@[email protected]) 
END 
GO 
+1

Il bit che stavo facendo male stava chiamando 'functionname' e che avrebbe dovuto essere' selezionare dbo.functionname come columnname' – ilivewithian

0

Prova questo:

result = _connection.Query<dynamic>("SELECT dbo.functionName(" + fnParam + ")", commandType: CommandType.Text); 
Problemi correlati