Skip to content

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Readme.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,35 @@ Alternatively, you might prefer Frans Bouma's [RawDataAccessBencher](https://git
187187
Parameterized queries
188188
---------------------
189189

190-
Parameters are passed in as anonymous classes. This allow you to name your parameters easily and gives you the ability to simply cut-and-paste SQL snippets and run them in your db platform's Query analyzer.
190+
Parameters are usually passed in as anonymous classes. This allow you to name your parameters easily and gives you the ability to simply cut-and-paste SQL snippets and run them in your db platform's Query analyzer.
191191

192192
```csharp
193193
new {A = 1, B = "b"} // A will be mapped to the param @A, B to the param @B
194194
```
195+
Parameters can also be built up dynamically using the DynamicParameters class. This allows for building a dynamic SQL statement while still using parameters for safety and performance.
195196

197+
```csharp
198+
var sqlPredicates = new List<string>();
199+
var queryParams = new DynamicParameters();
200+
if (boolExpression)
201+
{
202+
sqlPredicates.Add("column1 = @param1");
203+
queryParams.Add("param1", dynamicValue1, System.Data.DbType.Guid);
204+
} else {
205+
sqlPredicates.Add("column2 = @param2");
206+
queryParams.Add("param2", dynamicValue2, System.Data.DbType.String);
207+
}
208+
```
209+
210+
DynamicParameters also supports copying multiple parameters from existing objects of different types.
211+
212+
```csharp
213+
var queryParams = new DynamicParameters(objectOfType1);
214+
queryParams.AddDynamicParams(objectOfType2);
215+
```
216+
217+
When an object that implements the `IDynamicParameters` interface passed into `Execute` or `Query` functions, parameter values will be extracted via this interface. Obviously, the most likely object class to use for this purpose would be the built-in `DynamicParameters` class.
218+
196219
List Support
197220
------------
198221
Dapper allows you to pass in `IEnumerable<int>` and will automatically parameterize your query.

0 commit comments

Comments
 (0)