Count
This method counts the number of rows in a table.
Code Snippets
The following example counts rows in the [dbo].[Person] table inserted since yesterday.
using (var connection = new SqlConnection(connectionString))
{
var counted = connection.Count<Person>(e.DateInsertedUtc >= DateTime.UtcNow.Date.AddDays(-1));
}
Targeting a Table
To target a specific table, pass the literal table name.
using (var connection = new SqlConnection(connectionString))
{
var counted = connection.Count("[dbo].[Person]", new { State = "Michigan" });
}
Use QueryGroup or QueryField to build more complex WHERE expressions.
using (var connection = new SqlConnection(connectionString))
{
var where = new []
{
new QueryField("DateInsertedUtc", Operation.GreaterThanOrEqual, DateTime.UtcNow.Date.AddDays(-1))
}
var counted = connection.Count("[dbo].[Person]", where: where);
}
Table Hints
Pass a table hint via the hints argument.
using (var connection = new SqlConnection(connectionString))
{
var counted = connection.Count<Person>(e.DateInsertedUtc >= DateTime.UtcNow.Date.AddDays(-1),
hints: "WITH (NOLOCK)");
}
Or use the SqlServerTableHints class.
using (var connection = new SqlConnection(connectionString))
{
var counted = connection.Count<Person>(e.DateInsertedUtc >= DateTime.UtcNow.Date.AddDays(-1),
hints: SqlServerTableHints.NoLock);
}