[转][Dapper]SQL 经验集

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 = "123" }, tran);
        conn.Execute("update table2 set ic='3' where id=@a", new { a = "1234" }, tran);
        tran.Commit();
    }
}

这是一个使用 Dapper 调用事务的例子。

posted on 2017-06-09 23:01  z5337  阅读(180)  评论(1编辑  收藏  举报