0

I am facing one problem in asp.net Application with sql server database. I am getting Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. In my code stored procedure is taking around 43 seconds and after 30 seconds I am getting this error. For Solving this I have analyzed many sites and found these solutions:

  1. I need to set Connection Timeout=300 in connectionString in web.config file. This point I have done, but still I get the same error.

  2. I also need to set commandTimeout through code. My problem : I am not able to modify the default commandTimeout Because I am using DataTier_Using_SQLClient to connect to database. This does not contain the commandTimeout property .

    Actually default command timeout is 30 seconds only.

4

2 回答 2

1

假设您正在使用适配器

DataSet dsData = new DataSet();
SqlConnection conn = new SqlConnection(GetConnection());
SqlDataAdapter adapter = new SqlDataAdapter(strQuery, conn);
adapter.SelectCommand.CommandTimeout = 120;
于 2013-08-12T09:23:30.557 回答
0

非常感谢 Tim B James 和 January 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) 对象。

于 2013-08-13T04:35:08.700 回答