ADO

Ado 是 IFreeSql 下重要的对象之一,它包括所有对 SQL 操作的封装,提供 ExecuteReader、ExecuteDataSet、ExecuteDataTable、ExecuteNonQuery、ExecuteScalar 等方法,使用起来和传统 SqlHelper 一样。

查询 SQL 返回实体

  1. //返回多条记录
  2. List<T> list = fsql.Ado.Query<T>("select * from t1");
  3. //返回单条记录
  4. T item = fsql.Ado.QuerySingle<T>("select * from t1 where id = @id", new { id = 1 });
  5. //返回多个结果集
  6. var result = fsql.Ado.Query<T1, T2>("select * from t1; select * from t2");
  7. List<T1> list1 = result.Item1;
  8. List<T2> list2 = result.Item2;
  9. // like 查询
  10. string searchText = "abc";
  11. List<T> users = _fsql.Ado.Query<T>("select * from t1 where name like @name", new { name = "%" + searchText + "%" });

参数化

Ado 下面所有参数 object parms 都可以接受匿名对象,或者字典:

  • new { id = 1, name = “xx” }
  • new Dictionary\ { [“id”] = 1, [“name”] = “xx” }

关于参数前缀:

  • odbc 是 ? 并且没有标识,所以freesql禁用了 odbc 参数化
  • oracle 是 :
  • mysql.data 是 ?
  • mysqlconnector 是 @
  • 其他基本都是 @

IN 参数化查询:

当前仅支持Array和IList类型绑定

  1. var ids = new int[] { 1,2,3 };
  2. List<T> list = fsql.Ado.Query<T>("select * from t1 where id in @ids", new { ids = ids });

检测连接

  1. bool isok = fsql.Ado.ExecuteConnectTest();

CommandFluent

fsql.Ado 重载方法太多的情况下,建议使用 CommandFluent,例如存储过程:

  1. DbParameter p2 = null;
  2. fsql.Ado.CommandFluent("dbo.GetICMaxNum")
  3. .CommandType(CommandType.StoredProcedure)
  4. .CommandTimeout(60)
  5. .WithParameter("TableName", "tb1")
  6. .WithParameter("FInterID", null, p =>
  7. {
  8. p2 = p; //Output 参数
  9. p.DbType = DbType.Int32;
  10. p.Direction = ParameterDirection.Output;
  11. })
  12. .ExecuteNonQuery(); //.Query<T>() 或者 .ExecuteDataTable() 或者 ...
  13. Console.WriteLine(p2.Value);

Ado.net 扩展方法

提供了类似 Dapper 的使用方法,FreeSql 增加了 IDbConnection/IDbTransaction 对象的扩展方法 Select/Insert/Update/Delete 实现 CRUD。

  1. using FreeSql;
  2. using (var conn = new SqlConnection(...))
  3. {
  4. //IFreeSql fsql = conn.GetIFreeSql();
  5. //fsql.CodeFirst.IsNoneCommandParameter = true;
  6. //fsql.CodeFirst.IsSyncStructureToUpper = true;
  7. //fsql.Aop.CommandBefore += (_, e) => Trace.WriteLine(e.Command.CommandText);
  8. //以上整个程序只需要设置一次
  9. conn.Select<T>().Where(...).ToList();
  10. conn.Insert(new T {}).ExecuteAffrows();
  11. conn.Update().SetSource(new T {}).ExecuteAffrows();
  12. conn.InsertOrUpdate().SetSource(new T {}).ExecuteAffrows();
  13. conn.Delete<T>().Where(...).ExecuteAffrows();
  14. }
  • 每个 SqlConnection GetFreeSql() 返回的 IFreeSql 实例相同;
  • 可以对 fsql 设置 Aop 事件,比如监视 SQL;
  • IFreeSql 自身的成员 IDbFirst、Transaction 不可用;

利用本功能可以快速将 FreeSql 使用到项目中,只需要处理好实体类的特性。

提示:FreeSql 兼容 EFCore 99% 的实体特性