首页 > 数据库技术 > 详细

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

时间:2015-02-11 20:33:00      阅读:304      评论:0      收藏:0      [点我收藏+]
技术分享
 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

 

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

原文:http://www.cnblogs.com/lion0323/p/4286903.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!