1 Public Static BOOLTestdatabaseconnection (stringServerstringconnectionString)2 {3 BOOLIssuccess =false;4 5 using(SqlConnection connection =NewSqlConnection (connectionString))6 {7 using(SqlCommand command =NewSqlCommand ("", connection))8 {9 TryTen { OnePing Pingsender =NewPing (); APingOptions options =Newpingoptions (); -Options. Dontfragment =true; - byte[] buffer = Encoding.ASCII.GetBytes ("Test"); thepingreply reply = pingsender.send (server, -, buffer, options); - if(reply. Status! =ipstatus.success) - { - return false; + } - + connection. Open (); ACommand.commandtimeout =3; at if(Connection. state = =ConnectionState.Open) - { - connection. Close (); -Issuccess =true; - } - Else in { -Issuccess =false; to } + } - Catch(Exception ex) the { * Ex. Message.tostring (); $ return false;Panax Notoginseng } - the returnissuccess; + } A } the}
View Code
Database connection test Wait time setting