Database.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using Npgsql;
  3. namespace RestaurantApp.DAL
  4. {
  5. public static class Database
  6. {
  7. private const string Host = "edu-pg.itiscaf.ru";
  8. private const int Port = 5432;
  9. private const string DbName = "dbwork";
  10. private const string User = "shagavaliev_iv";
  11. private const string Password = "75&n057$380";
  12. private const string Schema = "shagavaliev_iv";
  13. public static string ConnectionString =>
  14. $"Host={Host};Port={Port};Database={DbName};Username={User};Password={Password};Search Path={Schema};";
  15. public static NpgsqlConnection GetConnection()
  16. {
  17. var conn = new NpgsqlConnection(ConnectionString);
  18. conn.Open();
  19. return conn;
  20. }
  21. public static bool TestConnection(out string error)
  22. {
  23. error = "";
  24. try
  25. {
  26. using (var conn = GetConnection()) { }
  27. return true;
  28. }
  29. catch (Exception ex)
  30. {
  31. error = ex.Message;
  32. return false;
  33. }
  34. }
  35. }
  36. }