带有参数列表的Dapper查询

时间:2012-11-08 15:03:04

标签: c# mysql database dapper

我正在尝试使用Dapper运行一个带有一组已知参数的查询,但是这些参数的值列表。我想要做的一个简单例子是:

DateTime endDate = DateTime.Now;
DateTime startDate = endDate.AddHours(-24);

string query = "select COUNT(*) from Test where Status = @Status AND DateCreated <= @Hour;";
var stuff = con.Query(query, (startDate).ByHourTo(endDate).Select(hour => new
{
     Status = 1,
     Hour = hour,
}));

Dapper抛出一个异常'参数'@Status'必须定义'。我知道Dapper在进行批量插入和更新时可以处理参数列表,但它是否可以不进行选择?

4 个答案:

答案 0 :(得分:18)

试试这个:

List<string> names = new List<string> { "Bob", "Fred", "Jack" };
string query = "select * from people where Name in @names";
var stuff = connection.Query<ExtractionRecord>(query, new {names});

答案 1 :(得分:10)

啊,我想我明白你的意思......

是的,我们支持查询不支持Execute的方案,特别是:使用一系列不同的参数值顺序运行相同的操作。这对Execute有意义,但对于查询,这可能意味着您应该使用in查看不同的查询。或者,只需循环和连接。

相反,它正在查看单个参数对象并查找公共值 - 可枚举的dapper没有任何合适的参数值。

答案 2 :(得分:2)

我知道我迟到了这个派对,但我想我理解这个请求意味着您只想传递一些属性并根据这些动态属性生成查询。

使用下面的代码我可以使用任何类型,然后填充并传入该类型的对象,并设置一些值(我称之为我的查询对象),并生成查询以查找匹配的对象您在查询对象中设置的值。

*注意bool和具有默认值的东西。

动态查询示例

    public IEnumerable<T> Query<T>(T templateobject) {
        var sql = "SELECT * From " + typeof(T).Name + " Where ";

        var list = templateobject.GetType().GetProperties()
             .Where(p => p.GetValue(templateobject) != null)
             .ToList();

        int i = 0;

        Dictionary<string, object> dbArgs = new Dictionary<string, object>();

        list.ForEach(x =>
        {
            sql += x.Name + " = @" +  x.Name;

            dbArgs.Add(x.Name, x.GetValue(templateobject));

            if (list.Count > 1 && i < list.Count - 1) {
                sql += " AND ";
                i++;
            }
        });

        Debug.WriteLine(sql);

        return _con.Query<T>(sql, dbArgs).ToList();
    }

<强>用法

* repo是包含上述功能的类

var blah = repo.Query<Domain>(new Domain() { Id = 1, IsActive=true });

<强>输出

SELECT * From Domain Where Id = @Id AND IsActive = @IsActive

然后它会吐出与上述查询匹配的任何“域名”。

答案 3 :(得分:0)

DECLARE @Now datetime
SET @Now = getdate()

SELECT
    DATEADD( hh, -n, @Now ) AS StartDate,
    DATEADD( hh, -n+1, @Now ) AS EndDate
INTO
    #DateRanges
FROM 
    Numbers
WHERE
    n <= 24

SELECT
    COUNT(*) AS [Count],
    #DateRanges.StartDate
FROM
    Test
        JOIN
    #DateRanges
        ON Test.DateCreated >= #DateRanges.StartDate
        AND Test.DateCreated < #DateRanges.EndDate
GROUP BY
    #DateRanges.StartDate

我将如何做到这一点,但这假设一件事:你的数据库中有一个名为&#34;数字&#34;其中包含任意数量的整数,每行一个,从1开始,其中至少包含24个数字。

也就是说,表格如下:

n
-----
1
2
3
4
5
...

如果您没有这样的表格,那么只需要这个命令就可以非常快速和轻松地制作一个表格:

CREATE TABLE #Numbers
(
    n int
)

SET NOCOUNT ON

INSERT #Numbers values (1);
GO
INSERT #Numbers SELECT n + (SELECT COUNT(*) FROM #Numbers) FROM #Numbers
GO 16 --execute batch 16 times to create 2^16 integers.

您不能在存储过程中拥有多个批次,但可以在文本命令中使用。 GO 16运行前一批次16次。如果您需要在存储过程中使用此功能,则可以多次重复第二个INSERT命令,而不是使用批处理。对于这个特定的查询,2 ^ 16个整数是过度的,但它是一个我需要复制和粘贴的命令,2 ^ 16通常就足够了,而且速度太快以至于我通常都不愿意改变它。 GO 5将产生32个整数,这对于24个日期范围就足够了。

这是一个完整的脚本,说明了这一点:

--Create a temp table full of integers. This could also be a static 
--table in your DB. It's very handy.
--The table drops let us run this whole script multiple times in SSMS without issue.
IF OBJECT_ID( 'tempdb..#Numbers' ) IS NOT NULL
    DROP TABLE #Numbers

CREATE TABLE #Numbers
(
    n int
)

SET NOCOUNT ON

INSERT #Numbers values (1);
GO
INSERT #Numbers SELECT n + (SELECT COUNT(*) FROM #Numbers) FROM #Numbers
GO 16 --execute batch 16 times to create 2^16 integers.

--Create our Test table. This would be the real table in your DB, 
-- so this would not go into your SQL command.
IF OBJECT_ID( 'tempdb..#Test' ) IS NOT NULL
    DROP TABLE #Test

CREATE TABLE #Test
(
    [Status] int,
    DateCreated datetime
)

INSERT INTO 
    #Test 
SELECT 
    1, 
    DATEADD( hh, -n, getdate() )
FROM 
    #Numbers
WHERE
    n <= 48

--#Test now has 48 records in it with one record per hour for 
--the last 48 hours.

--This drop would not be needed in your actual command, but I 
--add it here to make testing this script easier in SSMS.
IF OBJECT_ID( 'tempdb..#DateRanges' ) IS NOT NULL
    DROP TABLE #DateRanges

--Everything that follows is what would be in your SQL you send through Dapper 
--if you used a static Numbers table, or you might also want to include
--the creation of the #Numbers temp table.
DECLARE @Now datetime
SET @Now = getdate()

SELECT
    DATEADD( hh, -n, @Now ) AS StartDate,
    DATEADD( hh, -n+1, @Now ) AS EndDate
INTO
    #DateRanges
FROM 
    #Numbers
WHERE
    n <= 24

/* #DateRanges now contains 24 rows that look like this:

StartDate               EndDate
2016-08-04 15:22:26.223 2016-08-04 16:22:26.223
2016-08-04 14:22:26.223 2016-08-04 15:22:26.223
2016-08-04 13:22:26.223 2016-08-04 14:22:26.223
2016-08-04 12:22:26.223 2016-08-04 13:22:26.223
...

Script was run at 2016-08-04 16:22:26.223. The first row's end date is that time. 
This table expresses 24 one-hour datetime ranges ending at the current time. 
It's also  easy to make 24 one-hour ranges for one calendar day, or anything
similar.
*/

--Now we just join that table to our #Test table to group the rows those date ranges.

SELECT
    COUNT(*) AS [Count],
    #DateRanges.StartDate
FROM
    #Test
        JOIN
    #DateRanges
        ON #Test.DateCreated >= #DateRanges.StartDate
        AND #Test.DateCreated < #DateRanges.EndDate
GROUP BY
    #DateRanges.StartDate

/*
Since we used two different getdate() calls to populate our two tables, the last record of 
our #Test table is outside of the range of our #DateRange's last row by a few milliseconds,
so we only get 23 results from this query. This script is just an illustration.
*/