![[转][Dapper]SQL 经验集 [转][Dapper]SQL 经验集](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
condition.Append(" AND ChineseName like @name");
p.Add("@name", "%" + name + "%", System.Data.DbType.String);
like 也可以参数化查询。
@a MSSQL 的参数写法
:a Oracle 的参数写法
? OleDb 的参数写法
Access 中 * 表示模糊匹配
MSSQL / Oracle 中 % 表示模糊匹配
Access 默认的 OleDb 连接中 “Provider=Microsoft.Jet.OLEDB.4.0” 用的是 Access 2000,这个版本不支持 Replace 函数。
Dapper 是一个轻量的 ORM,可以方便的使用在 .net 项目中。
conn.Query<Users>("SELECT * FROM Users WHERE id IN @ids ",new { ids = new string[] { "a", "b" }}).ToList();
这样就可以达到 in 参数化查询的目地。
conn.QuerySingle<string>("select Count(*) from Users where str like @a", new { a = "%bx%" }, tran);
这样就可以达到 like 参数化查询的目地。
using (IDbConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True")) {
conn.Open();
using (IDbTransaction tran = conn.BeginTransaction()) {
conn.Execute("update table1 set ic='1' where id=@a", new { a = "" }, tran);
conn.Execute("update table2 set ic='3' where id=@a", new { a = "" }, tran);
tran.Commit();
}
}
这是一个使用 Dapper 调用事务的例子。