1.數據庫連接字符串中添加對數據庫的訪問時間 Connect Timeout=500 單位秒。默認30秒
eg.
<add key="ConnectionString" value="Data Source=1.1.1.1;Initial Catalog=aa;User ID=sa;PassWord=psd;Connect Timeout=500" />
2.web.config中添加executionTimeout 時間,指示在請求被 ASP.NET 自動關閉前允許執行的最大秒數。單位秒 默認90秒,加在httpRuntime標簽中。
eg.
<httpRuntime targetFramework="4.6" executionTimeout="500" />
3.在接口中設置連接時間,給SqlCommand 的實體設置CommandTimeout 時間.單位秒
eg.
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["SqlConnStr"]))
{
cnn.Open();
SqlCommand command = new SqlCommand();//因需要加長超時時間,使用sqlcommand方法
command.CommandTimeout = 600;//十分鍾
//指定Command對象所使用的Connection對象
command.Connection = cnn;
//指定Command對象用於執行SQL語句
command.CommandType = CommandType.Text;
//指定要執行的SQL語句
command.CommandText = totalCountSql;//要執行的SQL語句
//執行查詢操作,返回單個值
var count = command.ExecuteScalar().ToString();
dr1.Close();
cnn.Close();
}