Advertisement
Guest User

BaseDatabase

a guest
Jul 17th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. public class BaseDatabase
  2.     {
  3.         private IConfiguration _configuration;
  4.         private readonly ILogger<BaseDatabase> _logger;
  5.  
  6.         private static string connectionString = "";
  7.  
  8.         public BaseDatabase(IConfiguration configuration, ILogger<BaseDatabase> logger)
  9.         {
  10.             _configuration = configuration;
  11.             _logger = logger;
  12.         }
  13.  
  14.         public MySqlConnection GetOpenConnection()
  15.         {
  16.             if (connectionString.Length == 0)
  17.             {
  18.                 connectionString = _configuration.GetConnectionString("DefaultConnection");
  19.             }
  20.  
  21.             MySqlConnection connection = new MySqlConnection(connectionString);
  22.             try
  23.             {
  24.                 connection.Open();
  25.             }
  26.             catch (Exception ex)
  27.             {
  28.                 _logger.LogError(ex.ToString());
  29.             }
  30.  
  31.             return connection;
  32.         }
  33.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement