Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. var db = new MyEntityCollection();
  2.  
  3. try
  4. {
  5. var checkworking = from c in db.Customers select c;
  6. }
  7. catch
  8. {
  9. ConnectToBackUp();
  10. }
  11.  
  12. private bool TestConnection()
  13. {
  14. var db = new MyEntityCollection();
  15. int oldTimeOut = db.CommandTimeout;
  16.  
  17. try
  18. {
  19. db.CommandTimeout = 1;
  20. db.Connection.Open(); // check the database connection
  21. return true;
  22. }
  23. catch
  24. {
  25. return false;
  26. }
  27. finally
  28. {
  29. db.CommandTimeout = oldTimeOut;
  30. }
  31. }
  32.  
  33. using System.Data.Common;
  34. ...
  35.  
  36. public void TestConnection() {
  37. using (var db = new MyEntityCollection()) {
  38. DbConnection conn = db.Database.Connection;
  39. try {
  40. conn.Open(); // check the database connection
  41. return true;
  42. }
  43. catch {
  44. return false;
  45. }
  46. }
  47. }
  48.  
  49. private bool TestConnection()
  50. {
  51. EntityConnectionStringBuilder b = new EntityConnectionStringBuilder();
  52. ConnectionStringSettings entityConString = ConfigurationManager.ConnectionStrings["MyEntityConnectionString"];
  53. b.ConnectionString = entityConString.ConnectionString;
  54. string providerConnectionString = b.ProviderConnectionString;
  55.  
  56. SqlConnectionStringBuilder conStringBuilder = new SqlConnectionStringBuilder();
  57. conStringBuilder.ConnectionString = providerConnectionString;
  58. conStringBuilder.ConnectTimeout = 1;
  59. string constr = conStringBuilder.ConnectionString;
  60.  
  61. using (SqlConnection conn = new SqlConnection(constr))
  62. {
  63. try
  64. {
  65. conn.Open();
  66. return true;
  67. }
  68. catch
  69. {
  70. return false;
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement