Dapper能否从SQL函数返回值?

19

我有一个返回INT的SQL函数,当我尝试通过dapper调用它时,总是得不到结果。

我是这样调用它的:

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

Dapper是否支持SQL函数?

2个回答

14

Dapper应该支持它。你确定你的函数在正确的数据库中吗?

这是一个快速的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

以下是我在使用相同数据库连接创建的函数。

CREATE FUNCTION fx_SumTwoValues
( @Val1 int, @Val2 int )
RETURNS int
AS
BEGIN
  RETURN (@Val1+@Val2)
END
GO

1
我做错的部分是调用了 functionname,而应该是 select dbo.functionname as columnname - ilivewithian
@hellboy ODP.NET(或者你使用的任何驱动程序)抛出的任何异常都会传播到调用者。 - Alex

8

可以使用这个:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接