DatabaseDemo.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Apq.Cfg.Database;
  2. namespace Apq.Cfg.Samples.Demos;
  3. /// <summary>
  4. /// 示例 10: 数据库配置源
  5. /// 注意:需要运行数据库服务才能执行此示例
  6. /// </summary>
  7. public static class DatabaseDemo
  8. {
  9. public static async Task RunAsync(string baseDir)
  10. {
  11. Console.WriteLine("═══════════════════════════════════════════════════════════════");
  12. Console.WriteLine("示例 10: 数据库配置源");
  13. Console.WriteLine("═══════════════════════════════════════════════════════════════\n");
  14. Console.WriteLine("注意:此示例需要运行数据库服务\n");
  15. // 使用 SQLite 作为示例(无需额外服务)
  16. var dbPath = Path.Combine(baseDir, "config.db");
  17. try
  18. {
  19. Console.WriteLine("10.1 使用 SQLite 数据库配置源:");
  20. Console.WriteLine($" 数据库文件: {dbPath}");
  21. var cfg = new CfgBuilder()
  22. .AddDatabase(options =>
  23. {
  24. options.Provider = "SQLite";
  25. options.ConnectionString = $"Data Source={dbPath}";
  26. options.Table = "AppConfig";
  27. options.KeyColumn = "ConfigKey";
  28. options.ValueColumn = "ConfigValue";
  29. }, level: 0, isPrimaryWriter: true)
  30. .Build();
  31. Console.WriteLine(" 已连接到 SQLite 数据库");
  32. // 写入配置
  33. Console.WriteLine("\n10.2 写入配置到数据库:");
  34. cfg.Set("App:Name", "DatabaseApp");
  35. cfg.Set("App:Version", "1.0.0");
  36. cfg.Set("Database:MaxConnections", "100");
  37. await cfg.SaveAsync();
  38. Console.WriteLine(" 已写入 3 个配置项");
  39. // 读取配置
  40. Console.WriteLine("\n10.3 从数据库读取配置:");
  41. Console.WriteLine($" App:Name = {cfg.Get("App:Name")}");
  42. Console.WriteLine($" App:Version = {cfg.Get("App:Version")}");
  43. Console.WriteLine($" Database:MaxConnections = {cfg.Get<int>("Database:MaxConnections")}");
  44. // 修改配置
  45. Console.WriteLine("\n10.4 修改配置:");
  46. cfg.Set("App:Version", "2.0.0");
  47. await cfg.SaveAsync();
  48. Console.WriteLine($" 修改后 App:Version = {cfg.Get("App:Version")}");
  49. // 删除配置
  50. Console.WriteLine("\n10.5 删除配置:");
  51. cfg.Remove("App:Name");
  52. cfg.Remove("App:Version");
  53. cfg.Remove("Database:MaxConnections");
  54. await cfg.SaveAsync();
  55. Console.WriteLine(" 已清理测试配置");
  56. cfg.Dispose();
  57. // 等待数据库连接完全释放
  58. await Task.Delay(100);
  59. GC.Collect();
  60. GC.WaitForPendingFinalizers();
  61. // 清理数据库文件
  62. try
  63. {
  64. if (File.Exists(dbPath))
  65. File.Delete(dbPath);
  66. }
  67. catch
  68. {
  69. // 忽略删除失败
  70. }
  71. Console.WriteLine("\n10.6 其他数据库支持:");
  72. Console.WriteLine(" - SqlServer: Provider=\"SqlServer\"");
  73. Console.WriteLine(" - MySQL: Provider=\"MySql\"");
  74. Console.WriteLine(" - PostgreSQL: Provider=\"PostgreSQL\"");
  75. Console.WriteLine(" - Oracle: Provider=\"Oracle\"");
  76. Console.WriteLine("\n[示例 10 完成]\n");
  77. }
  78. catch (Exception ex)
  79. {
  80. Console.WriteLine($" [跳过] 数据库服务不可用: {ex.Message}");
  81. // 等待连接释放后再删除
  82. await Task.Delay(100);
  83. GC.Collect();
  84. GC.WaitForPendingFinalizers();
  85. try
  86. {
  87. if (File.Exists(dbPath))
  88. File.Delete(dbPath);
  89. }
  90. catch
  91. {
  92. // 忽略删除失败
  93. }
  94. Console.WriteLine("\n[示例 10 跳过]\n");
  95. }
  96. }
  97. }