2013-08-12 38 views
0

我面臨一個問題在asp.net應用程序與sql服務器數據庫。 我越來越Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. 在我的代碼存儲過程需要大約43秒和30秒後 我收到此錯誤。爲了解決這個我已經分析了很多網站,發現這些解決方案:Asp.net「超時已過,操作完成之前已超時或服務器沒有響應。」

  1. 我需要設置Connection Timeout=300中的connectionString在web.config文件。 這一點我已經完成了,但我仍然得到同樣的錯誤。

  2. 我還需要通過代碼設置commandTimeout。 我的問題:我無法修改默認commandTimeout 因爲我使用DataTier_Using_SQLClient連接到數據庫。 這不包含commandTimeout屬性。

    其實默認命令超時只有30秒。

+2

什麼是「DataTier_Using_SQLClient」? –

+0

@ user2674313你能提供用於檢索結果的代碼嗎? – Nilesh

回答

1

假設你正在使用適配器

DataSet dsData = new DataSet(); 
SqlConnection conn = new SqlConnection(GetConnection()); 
SqlDataAdapter adapter = new SqlDataAdapter(strQuery, conn); 
adapter.SelectCommand.CommandTimeout = 120; 
0

非常感謝您添乙詹姆斯和一月Mmako

其實我是解決與一個應用程序的一個問題。 DataTier_Using_SQLClient是我的應用程序中與數據庫連接的一個類。 然後忘了這堂課。

我已經通過使用SqlConnection創建連接來解決此問題。 請參閱下面的代碼。使用此代碼,你可以解決Time Expired錯誤 並且可以調用檢索具有一個參數,它需要通過存儲過程:

using System.Data; 

using System.Data.SqlClient; 

using System.Configuration; 

使用下面的代碼,你想

string connetionString = null; 

SqlConnection connection ; 

SqlDataAdapter adapter ; 

SqlCommand command = new SqlCommand(); 

SqlParameter param ; 

DataSet ds = new DataSet(); 

//You can specify this connectionString in web.config or here 

connetionString = "Data Source=servername; 
Initial Catalog=PUBS;User ID=sa; 
Password=yourpassword; 
Connection Timeout=300"; 


connection = new SqlConnection(connetionString); 


connection.Open(); 

command.Connection = connection; 

command.CommandType = CommandType.StoredProcedure; 

command.CommandText = "sp_Retrieve_ProcedureName"; 


param = new SqlParameter("@paramName", ParamValue); 

param.Direction = ParameterDirection.Input; 

param.DbType = DbType.Int32; 

command.Parameters.Add(param); 

adapter = new SqlDataAdapter(command); 


adapter.SelectCommand.CommandTimeout = 120; 


adapter.Fill(ds); 

您可以使用此ds (DataSet)對象,無論你想要什麼。

相關問題