1 public class SqlExecuter : ISqlExecuter, ITransientDependency
2 {
3 private IDbContextProvider<OADbContext> _dbContextProvider = null;
4
5 public SqlExecuter(IDbContextProvider<OADbContext> dbContextProvider)
6 {
7 _dbContextProvider = dbContextProvider;//IocManager.Instance.Resolve<IDbContextProvider<OADbContext>>();
8 }
9
10 /// <summary>
11 /// 执行给定的命令
12 /// </summary>
13 /// <param name="sql">命令字符串</param>
14 /// <param name="parameters">要应用于命令字符串的参数</param>
15 /// <returns>执行命令后由数据库返回的结果</returns>
16 public int Execute(string sql)
17 {
18 return _dbContextProvider.GetDbContext().Database.ExecuteSqlCommand(new RawSqlString(sql));
19 }
20
21 /// <summary>
22 /// 传入查询sql,返回List<T>数组
23 /// </summary>
24 /// <param name="sql"></param>
25 /// <returns></returns>
26 public async Task<List<T>> SqlQuery<T>(string sql) where T : class, new()
27 {
28 return await Task.Run(() =>
29 {
30 var db = _dbContextProvider.GetDbContext().Database;
31 var conn = db.GetDbConnection();
32 if (conn.State != ConnectionState.Open)
33 conn.Open();
34
35 var result = new List<T>();
36
37 try
38 {
39 RelationalDataReader query = null;
40
41 using (db.GetService<IConcurrencyDetector>().EnterCriticalSection())
42 {
43 var rawSqlCommand = db.GetService<IRawSqlCommandBuilder>().Build(sql);
44
45 query = rawSqlCommand.ExecuteReader(db.GetService<IRelationalConnection>());
46 }
47
48 //获取DbDataReader
49 var dr = query.DbDataReader;
50
51 var properties = typeof(T).GetProperties().ToList();
52
53 while (dr.Read())
54 {
55 var obj = new T();
56 foreach (var property in properties)
57 {
58 //获取该字段明的列序号,从0开始
59 var id = dr.GetOrdinal(property.Name.ToLower());
60
61 if (!dr.IsDBNull(id))
62 {
63 if (dr.GetValue(id) != DBNull.Value)
64 {
65 property.SetValue(obj, dr.GetValue(id));
66 }
67 }
68 }
69
70 result.Add(obj);
71 }
72
73 //关闭DbDataReader
74 dr.Close();
75 }
76 catch (Exception e)
77 {
78 throw new UserFriendlyException(e.Message);
79 }
80
81 return result;
82 });
83 }
84 }