2015-07-03 27 views
0

我得到这个错误:SQL调用超时 - Asp.net

The wait operation timed out 

而且它发生在这条线:

public static DataTable Get_Table_Grouped() 
{ 
    DataTable result = new DataTable(); 

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["My_Data"].ConnectionString)) 
    { 
     using (SqlCommand cmd = new SqlCommand(@" 
     SELECT TOP 10 Attribute1, Attribute2, Attribute3, Attribute4, Attribute5, 
     Sum([Attribute6]) As Attribute6 
     FROM My_Table 
     group by Attribute1, Attribute2, Attribute3, Attribute4, Attribute5", con)) 
     { 
      con.Open(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      da.Fill(result); 
      con.Close(); 
      da.Dispose(); 
     } 
    } 

    return result; 
} 

以下:

da.Fill(result); 

在下面的代码是我的连接字符串“My_Data”

<add name="My_Data" connectionString="Data Source=Database_Location;User ID=username;Password=my_password;" providerName="System.Data.SqlClient"/> 

我能做些什么来解决这个问题?有没有办法阻止超时?当我在Microsoft SQL Server中直接尝试相同的查询时,它可以正常工作,但过了一段时间后,我的代码中看不到任何可导致超时的内容

回答

1

超时错误意味着查询花费的时间超过了您定义的最长时间。您需要改进查询,更改超时值或检查表上是否存在锁。

你可以这样做更改超时值:

cmd.CommandTimeout = 600; 

你也可以通过添加在最后连接超时= 600指定连接字符串中的超时值。在你的情况下:

Data Source=Database_Location;User ID=username;Password=my_password;Connection Timeout=600; 
0

命令超时的默认值是30 seconds。如果您的查询拉入大量数据,或速度很慢,则必须手动设置超时值。旧金山提供了一对夫妇的可能性,但另一种方式,你可以增加超时是通过你的SqlDataAdapter例如:

da.SelectCommand.CommandTimeout = MyIntValue; 

而且,由于你与using声明,呼吁你SqlConnection,你con.Close()实际上是多余的。