一、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();