Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var db = new MyEntityCollection();
- try
- {
- var checkworking = from c in db.Customers select c;
- }
- catch
- {
- ConnectToBackUp();
- }
- private bool TestConnection()
- {
- var db = new MyEntityCollection();
- int oldTimeOut = db.CommandTimeout;
- try
- {
- db.CommandTimeout = 1;
- db.Connection.Open(); // check the database connection
- return true;
- }
- catch
- {
- return false;
- }
- finally
- {
- db.CommandTimeout = oldTimeOut;
- }
- }
- using System.Data.Common;
- ...
- public void TestConnection() {
- using (var db = new MyEntityCollection()) {
- DbConnection conn = db.Database.Connection;
- try {
- conn.Open(); // check the database connection
- return true;
- }
- catch {
- return false;
- }
- }
- }
- private bool TestConnection()
- {
- EntityConnectionStringBuilder b = new EntityConnectionStringBuilder();
- ConnectionStringSettings entityConString = ConfigurationManager.ConnectionStrings["MyEntityConnectionString"];
- b.ConnectionString = entityConString.ConnectionString;
- string providerConnectionString = b.ProviderConnectionString;
- SqlConnectionStringBuilder conStringBuilder = new SqlConnectionStringBuilder();
- conStringBuilder.ConnectionString = providerConnectionString;
- conStringBuilder.ConnectTimeout = 1;
- string constr = conStringBuilder.ConnectionString;
- using (SqlConnection conn = new SqlConnection(constr))
- {
- try
- {
- conn.Open();
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement