DbProvider.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Infrastructure;
  2. using Infrastructure.Model;
  3. using SqlSugar;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text.RegularExpressions;
  7. namespace ZR.CodeGenerator
  8. {
  9. /// <summary>
  10. /// 代码生成数据库连接
  11. /// </summary>
  12. public class DbProvider
  13. {
  14. protected static SqlSugarClient CodeDb;
  15. /// <summary>
  16. /// 获取动态连接字符串
  17. /// </summary>
  18. /// <param name="dbName">数据库名</param>
  19. /// <returns></returns>
  20. public SqlSugarClient GetSugarDbContext(string dbName = "")
  21. {
  22. DbConfigs configs = AppSettings.Get<DbConfigs>("CodeGenDbConfig");
  23. string connStr = configs.Conn;
  24. if (!string.IsNullOrEmpty(dbName))
  25. {
  26. configs.DbName = dbName;
  27. }
  28. connStr = connStr.Replace("{dbName}", configs.DbName, StringComparison.OrdinalIgnoreCase);
  29. var db = new SqlSugarClient(new List<ConnectionConfig>()
  30. {
  31. new ConnectionConfig(){
  32. ConnectionString = connStr,
  33. DbType = (DbType)configs.DbType,
  34. IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
  35. InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
  36. },
  37. });
  38. CodeDb = db;
  39. return db;
  40. }
  41. /// <summary>
  42. /// 获得字符串中开始和结束字符串中间得值
  43. /// </summary>
  44. /// <param name="str">字符串</param>
  45. /// <param name="s">开始</param>
  46. /// <param name="e">结束</param>
  47. /// <returns></returns>
  48. public static string GetValue(string str, string s, string e)
  49. {
  50. Regex rg = new("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
  51. return rg.Match(str).Value;
  52. }
  53. }
  54. }