![](/img/trans.png)
[英]How do I query a SQL Server database with a list of parameters in Dapper?
[英]Dapper query with list of parameters
我正在尝试使用具有已知参数集的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可以在进行批量插入和更新时处理参数列表,但是不能对选择执行此操作吗?
尝试这个:
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});
啊,我想我明白你的意思了...
是的,有一个情况下,我们支持Execute
不支持,对于查询,具体是:用各种不同的参数值的顺序执行相同的操作。 这对于Execute
很有意义,但对于查询,可能意味着您应该使用in
来查看其他查询。 或者,只需循环并连接。
取而代之的是,它正在查看单个参数对象并在寻找公共值-可枚举对象没有适用于dapper的任何合适的参数值。
我知道我参加这个聚会已经很晚了,但是,我想我理解这个要求意味着您只想传递一些属性并基于这些动态属性生成查询。
使用下面的代码,我可以使用任何类型,然后填充并传入具有几个值集的该类型的对象(我将此称为我的查询对象),然后将生成查询以查找与以下值匹配的对象您在查询对象中设置。
*请注意布尔值和具有默认值的内容。
动态查询示例
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
然后它会吐出与上述查询匹配的所有“域”。
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
我就是这样做的,但这假设了一件事情:数据库中有一个名为“ Numbers”的表,其中有任意数量的整数,每行一个,从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.
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.