ExecSqlService.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Bowin.Common.Log;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Text;
  6. using YLShipBuildLandMap.Entity;
  7. namespace YLShipBuildLandMap.Services.SystemSetting
  8. {
  9. public interface IExecSqlService
  10. {
  11. DataTable select(string sql);
  12. int insert(string sql);
  13. int update(string sql);
  14. int delete(string sql);
  15. }
  16. public class ExecSqlService: IExecSqlService
  17. {
  18. private YLShipBuildLandMapContext DbContext { get; set; }
  19. public ExecSqlService(YLShipBuildLandMapContext dbContext)
  20. {
  21. this.DbContext = dbContext;
  22. }
  23. public DataTable select(string sql)
  24. {
  25. if (!sql.ToLower().Contains("where"))
  26. throw new Exception("请添加where条件");
  27. LogHelper.WriteLog(LogType.CommmLog, $"exec sql:{sql}");
  28. return this.DbContext.Database.SqlQuery(sql);
  29. }
  30. public int insert(string sql)
  31. {
  32. if (!sql.ToLower().Contains("insert"))
  33. throw new Exception("缺少关键字insert");
  34. LogHelper.WriteLog(LogType.CommmLog, $"exec sql:{sql}");
  35. this.DbContext.Database.SqlQuery(sql);
  36. return 1;
  37. }
  38. public int update(string sql)
  39. {
  40. if (!sql.ToLower().Contains("update"))
  41. throw new Exception("缺少关键字update");
  42. if (!sql.ToLower().Contains("where"))
  43. throw new Exception("请添加where条件");
  44. LogHelper.WriteLog(LogType.CommmLog, $"exec sql:{sql}");
  45. this.DbContext.Database.SqlQuery(sql);
  46. return 1;
  47. }
  48. public int delete(string sql)
  49. {
  50. if (!sql.ToLower().Contains("delete"))
  51. throw new Exception("缺少关键字delete");
  52. if (!sql.ToLower().Contains("where"))
  53. throw new Exception("请添加where条件");
  54. LogHelper.WriteLog(LogType.CommmLog, $"exec sql:{sql}");
  55. this.DbContext.Database.SqlQuery(sql);
  56. return 1;
  57. }
  58. }
  59. }