|
| 1 | +# Query Methods |
| 2 | + |
| 3 | +## Execute or ExecuteAsync |
| 4 | + |
| 5 | +Executes the command against a connection and returns the number of rows affected. |
| 6 | + |
| 7 | +```csharp |
| 8 | +int result = -1; |
| 9 | +long total = -1; |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +using var session = Services.GetRequiredService<IDataSession>(); |
| 14 | + |
| 15 | +result = session |
| 16 | + .StoredProcedure("[dbo].[UserCountByEmailAddress]") |
| 17 | + .Parameter("@EmailAddress", email) |
| 18 | + .Return<long>(p => total = p) |
| 19 | + .Execute(); |
| 20 | +``` |
| 21 | + |
| 22 | + |
| 23 | +## Query or QueryAsync |
| 24 | + |
| 25 | +Executes the command against the connection and converts the results to a list of objects. |
| 26 | + |
| 27 | +```csharp |
| 28 | +long total = -1; |
| 29 | +var email = "%@battlestar.com"; |
| 30 | + |
| 31 | +using var session = Services.GetRequiredService<IDataSession>(); |
| 32 | + |
| 33 | +var users = session |
| 34 | + .StoredProcedure("[dbo].[UserListByEmailAddress]") |
| 35 | + .Parameter("@EmailAddress", email) |
| 36 | + .Parameter("@Offset", 0) |
| 37 | + .Parameter("@Size", 10) |
| 38 | + .Parameter<long>(parameter => parameter |
| 39 | + .Name("@Total") |
| 40 | + .Type(DbType.Int64) |
| 41 | + .Output(v => total = v) |
| 42 | + .Direction(ParameterDirection.Output) |
| 43 | + ) |
| 44 | + .Query<User>() // using source generated factor |
| 45 | + .ToList(); |
| 46 | +``` |
| 47 | + |
| 48 | +## QuerySingle or QuerySingleAsync |
| 49 | + |
| 50 | +Executes the query and returns the first row in the result as an object. |
| 51 | + |
| 52 | +```csharp |
| 53 | +var session = Services.GetRequiredService<IDataSession>(); |
| 54 | + |
| 55 | + |
| 56 | +var sql = "select * from [User] where EmailAddress = @EmailAddress"; |
| 57 | + |
| 58 | +var user = session.Sql(sql) |
| 59 | + .Parameter("@EmailAddress", email) |
| 60 | + .QuerySingle(r => new User |
| 61 | + { |
| 62 | + Id = r.GetGuid("Id"), |
| 63 | + EmailAddress = r.GetString("EmailAddress"), |
| 64 | + IsEmailAddressConfirmed = r.GetBoolean("IsEmailAddressConfirmed"), |
| 65 | + DisplayName = r.GetString("DisplayName"), |
| 66 | + PasswordHash = r.GetString("PasswordHash"), |
| 67 | + ResetHash = r.GetString("ResetHash"), |
| 68 | + InviteHash = r.GetString("InviteHash"), |
| 69 | + AccessFailedCount = r.GetInt32("AccessFailedCount"), |
| 70 | + LockoutEnabled = r.GetBoolean("LockoutEnabled"), |
| 71 | + LockoutEnd = r.GetDateTimeOffsetNull("LockoutEnd"), |
| 72 | + LastLogin = r.GetDateTimeOffsetNull("LastLogin"), |
| 73 | + IsDeleted = r.GetBoolean("IsDeleted"), |
| 74 | + Created = r.GetDateTimeOffset("Created"), |
| 75 | + CreatedBy = r.GetString("CreatedBy"), |
| 76 | + Updated = r.GetDateTimeOffset("Updated"), |
| 77 | + UpdatedBy = r.GetString("UpdatedBy"), |
| 78 | + RowVersion = r.GetBytes("RowVersion"), |
| 79 | + }); |
| 80 | +``` |
| 81 | + |
| 82 | +## QueryValue or QueryValueAsync |
| 83 | + |
| 84 | +Executes the query and returns the first column of the first row in the result set returned by the query asynchronously. All other columns and rows are ignored. |
| 85 | + |
| 86 | +```csharp |
| 87 | +await using var session = Services.GetRequiredService<IDataSession>(); |
| 88 | + |
| 89 | +string email = "@battlestar.com"; |
| 90 | + |
| 91 | +var count = await session |
| 92 | + .Sql(builder => builder |
| 93 | + .Select<User>() |
| 94 | + .Count() |
| 95 | + .Where(p => p.EmailAddress, email, FilterOperators.Contains) |
| 96 | + ) |
| 97 | + .QueryValueAsync<int>(); |
| 98 | +``` |
| 99 | + |
| 100 | +## QueryMultiple or QueryMultipleAsync |
| 101 | + |
| 102 | +Executes the command against the connection and sends the results for reading multiple results sets |
| 103 | + |
| 104 | +```csharp |
| 105 | +string email = "[email protected]"; |
| 106 | +string sql = "select * from [User] where EmailAddress = @EmailAddress; " + |
| 107 | + "select * from [Role]; " + |
| 108 | + "select * from [Priority]; "; |
| 109 | + |
| 110 | +User user = null; |
| 111 | +List<Role> roles = null; |
| 112 | +List<Priority> priorities = null; |
| 113 | + |
| 114 | +await using var session = Services.GetRequiredService<IDataSession>(); |
| 115 | + |
| 116 | +await session.Sql(sql) |
| 117 | + .Parameter("@EmailAddress", email) |
| 118 | + .QueryMultipleAsync(async q => |
| 119 | + { |
| 120 | + user = await q.QuerySingleAsync<User>(); |
| 121 | + roles = (await q.QueryAsync<Role>()).ToList(); |
| 122 | + priorities = (await q.QueryAsync<Priority>()).ToList(); |
| 123 | + }); |
| 124 | +``` |
0 commit comments