数据库连接测试等待时间设定

 1 public static bool TestDatabaseConnection(string server, string connectionString)
 2         {
 3             bool isSuccess = false;
 4 
 5             using (SqlConnection connection = new SqlConnection(connectionString))
 6             {
 7                 using (SqlCommand command = new SqlCommand("", connection))
 8                 {
 9                     try
10                     {
11                         Ping pingSender = new Ping();
12                         PingOptions options = new PingOptions();
13                         options.DontFragment = true;
14                         byte[] buffer = Encoding.ASCII.GetBytes("test");
15                         PingReply reply = pingSender.Send(server, 120, buffer, options);
16                         if (reply.Status != IPStatus.Success)
17                         {
18                             return false;
19                         }
20 
21                         connection.Open();
22                         command.CommandTimeout = 3;
23                         if (connection.State == ConnectionState.Open)
24                         {
25                             connection.Close();
26                             isSuccess = true;
27                         }
28                         else
29                         {
30                             isSuccess = false;
31                         }
32                     }
33                     catch (Exception ex)
34                     {
35                         ex.Message.ToString();
36                         return false;
37                     }
38 
39                     return isSuccess;
40                 }
41             }
42         }
View Code

 

posted @ 2015-02-11 19:59  听雨萧潇  阅读(315)  评论(0编辑  收藏  举报