C#测试数据库连接是否成功

新建ConnectionTestInfo类
using System.Data.SqlClient;
using System.Data;

public class ConnectionTestInfo
    {
        private static SqlConnection mySqlConnection;  //mySqlConnection   is   a   SqlConnection   object
        private static string ConnectionString = "";
        private static bool IsCanConnectioned = false;

        /// <summary>
        /// 测试连接数据库是否成功
        /// </summary>
        /// <returns></returns>
        public static bool ConnectionTest()
        {
            //获取数据库连接字符串
            ConnectionString = ConnectionInfo.ConnectionString();
            //创建连接对象
            mySqlConnection = new SqlConnection(ConnectionString);
            //ConnectionTimeout 在.net 1.x 可以设置 在.net 2.0后是只读属性,则需要在连接字符串设置
            //如:server=.;uid=sa;pwd=;database=PMIS;Integrated Security=SSPI; Connection Timeout=30
            //mySqlConnection.ConnectionTimeout = 1;//设置连接超时的时间
            try
            {
                //Open DataBase
                //打开数据库
                mySqlConnection.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                //Can not Open DataBase
                //打开不成功 则连接不成功
                IsCanConnectioned = false;
            }
            finally
            {
                //Close DataBase
                //关闭数据库连接
                mySqlConnection.Close();
            }
            //mySqlConnection   is   a   SqlConnection   object
            if (mySqlConnection.State == ConnectionState.Closed || mySqlConnection.State == ConnectionState.Broken)
            {
                //Connection   is   not   available 
                return IsCanConnectioned;
            }
            else
            {
                //Connection   is   available 
                return IsCanConnectioned;
            }
        }
    }

其中数据库字符串调用了类ConnectionInfo的方法ConnectionString
public class ConnectionInfo
    {
        public ConnectionInfo() { }

        /// <summary>
        /// 从配置文件中读取数据库联接字符串
        /// </summary>
        /// <returns></returns>
        public static string ConnectionString()
        {
            return (ConfigurationSettings.AppSettings["ConnectionString"]);
        }

    }

 

 

 

解决ASP.NET Web Applicatio超时时间已到.在操作完成之前超时时间已过或服务器未响应

 

“超时时间已到。在操作完成之前超时时间已过或服务器未响应”
初步分析原因为对MSSQL操作时连接超时,知道这事,以前没留意,大概是在配置文件中设置连接时限,在网上找了下解决方法,大多说在数据库连接字符串里解决
SqlConnection con = new SqlConnection("server=.;database=myDB;uid=sa;pwd=password;")改为:
SqlConnection con = new SqlConnection("server=.;database=myDB;uid=sa;pwd=password;Connect Timeout=500")似乎没效果。依然运行30秒即报超时!
突然感觉似乎应该可以在连接数据库代码中指明,式了下con的属性,有个ConnectionTimeout,

SqlConnection con = new SqlConnection("server=.;database=myDB;uid=sa;pwd=;");
con.ConnectionTimeout 
= 180;//报错,属性ConnectionTimeout 为只读!尝试失败,再接着看command对象属性,发现其也有类似属性!CommandTimeout设置一下:
SqlCommand cmd = new SqlCommand();
cmd.CommandTimeout 
= 180;再运行,即解决,这里设置的时间的180秒,即三分钟!可根据需要设置,如果过长,也可以设置为0,当此属性设置为0时表示不限制时间。此属性值应该慎用。还需要在Web.config配置文件中设置http请求运行时限间
<system.web>  
<httpRuntime maxRequestLength="102400" executionTimeout="720" />
</system.web>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值