执行存储过程,或者sql语句超时,解决办法

一、SqlHelper 超时解决方案

1、连接字符串中添加超时时间:Connect Timeout=30//默认为30(秒)修改大点;
2、修改 SQLHELPER.cs 中PrepareCommand 方法修改如下
        private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection )
        {
    if( command == null ) throw new ArgumentNullException( "command" );
    if( commandText == null || commandText.Length == 0 ) throw new ArgumentNullException( "commandText" );

            // If the provided connection is not open, we will open it
    if (connection.State != ConnectionState.Open)
    {
     mustCloseConnection = true;
     connection.Open();
    }
    else
    {
     mustCloseConnection = false;
    }

            // Associate the connection with the command
            command.Connection = connection;

            // Set the command text (stored procedure name or SQL statement)
            command.CommandText = commandText;
            //添加下面这句;
            command.CommandTimeout = connection.ConnectionTimeout;

            // If we were provided a transaction, assign it
            if (transaction != null)
            {
     if( transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );
                command.Transaction = transaction;
            }

            // Set the command type
            command.CommandType = commandType;

            // Attach the command parameters if they are provided
            if (commandParameters != null)
            {
                AttachParameters(command, commandParameters);
            }
            return;
        }


二、使用DbCommand,设置CommandTimeout的时间大一点,默认是30秒

如:

            Database db = DB.XXReader;
            DbCommand cmd = db.GetStoredProcCommand("XXXXXX");
            cmd.CommandTimeout = 720;
            DataTable tbl = db.ExecuteDataSet(cmd).Tables[0];
            cmd.Dispose();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值