25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

ConnectionUtil.cs 1.3 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using MySql.Data.MySqlClient;
  3. namespace DotNetCore.CAP.MySql.Test
  4. {
  5. public static class ConnectionUtil
  6. {
  7. private const string ConnectionStringTemplateVariable = "Cap_MySql_ConnectionString";
  8. private const string MasterDatabaseName = "information_schema";
  9. private const string DefaultDatabaseName = "cap_test";
  10. private const string DefaultConnectionString =
  11. @"Server=localhost;Database=cap_test;Uid=root;Pwd=123123;Allow User Variables=True;SslMode=none;";
  12. public static string GetDatabaseName()
  13. {
  14. return DefaultDatabaseName;
  15. }
  16. public static string GetMasterConnectionString()
  17. {
  18. return GetConnectionString().Replace(DefaultDatabaseName, MasterDatabaseName);
  19. }
  20. public static string GetConnectionString()
  21. {
  22. return
  23. Environment.GetEnvironmentVariable(ConnectionStringTemplateVariable) ??
  24. DefaultConnectionString;
  25. }
  26. public static MySqlConnection CreateConnection(string connectionString = null)
  27. {
  28. connectionString ??= GetConnectionString();
  29. var connection = new MySqlConnection(connectionString);
  30. connection.Open();
  31. return connection;
  32. }
  33. }
  34. }