问题
This SqlBuilder:
var builder = new SqlBuilder();
var sql = builder.AddTemplate( /*...
Intensely dumb question but, how do I use this? I know it's in Dapper.Contrib
, but that using
statement isn't enough. What references or other using
statements do I need to add?
回答1:
This question appears in the dapper tutorial page, so I'm updating the answer.
In version 1.6, SqlBuilder
is in the namespace Dapper
. And it is included in the nuget package Dapper.SqlBuilder.
This is an example of how it works:
var builder = new SqlBuilder();
builder.Select("id_something");
builder.Select("MyCol");
builder.Select("OtherCol");
DynamicParameters parameters = new DynamicParameters();
parameters.Add("@MyParam", 3, DbType.Int32, ParameterDirection.Input);
builder.Where("id_something < @MyParam", parameters);
// builder.Where("id_something < @MyParam", new { MyParam =3}); //this is other option for params.
builder.InnerJoin("OtherTable on OtherTable.id=MyTable.id");
//The /**something**/ are placeholders,
var builderTemplate = builder.AddTemplate("Select /**select**/ from MyTable /**innerjoin**/ /**where**/ ");
var result = connection.Query<MyClass>(builderTemplate.RawSql, builderTemplate.Parameters);
This is the Sql generated:
Select id_something , MyCol , OtherCol
from MyTable
INNER JOIN OtherTable on OtherTable.id=MyTable.id
WHERE id_something < @MyParam
回答2:
To this date, SqlBuilder has not made yet to any official package on Nuget. I got that installed by using the following package: Dapper.SqlBuilder. I really hope it becomes part of the official library.
Anyways, to your question, once the package is installed, you don't need to add any new "using" clause, as the SqlBuilder is inside the same Dapper namespace.
You can check the source code details in here: https://github.com/StackExchange/dapper-dot-net/blob/master/Dapper.SqlBuilder/SqlBuilder.cs
回答3:
Don't know if no one knew or if it was just too silly to answer, but this is the correct statement:
using StackExchange.Profiling.Helpers.Dapper;
回答4:
I did figure it out by making a new class file named SqlBuilder.cs and copied content from THIS link
来源:https://stackoverflow.com/questions/19009550/how-to-use-sqlbuilder