====请教Sql server的Timeout Expired的问题==========

blueskywjs 2009-02-02 09:39:04
我的一个Sql语句在查询分析器中执行的时间是3分钟,但是在程序就打开却出现错误:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

Source Error:

部分程序如下:

Dim ConnStr As String
ConnStr = System.Configuration.ConfigurationSettings.AppSettings("kingseer_dbConnectionString").ToString
Dim MyConnection As SqlClient.SqlConnection
MyConnection = New SqlClient.SqlConnection(ConnStr)
MyConnection.Open()
string sql="select * from table"
Dim dsdataSet As New DataSet1()

Dim daOrders As New Data.SqlClient.SqlDataAdapter(sql, MyConnection)
daOrders.Fill(dsdataSet, "t_biao_cnskb")

'使用“报表引擎”对象模型将填充的数据集,传递给报表
oRpt.SetDataSource(dsdataSet)
CrystalReportViewer1.ReportSource = oRpt



请回答,谢谢!




...全文
558 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueskywjs 2009-02-02
  • 打赏
  • 举报
回复
具体怎么写呢?我是新手。谢谢
Dim ConnStr As String
ConnStr = System.Configuration.ConfigurationSettings.AppSettings("kingseer_dbConnectionString").ToString
Dim MyConnection As SqlClient.SqlConnection

MyConnection = New SqlClient.SqlConnection(ConnStr)

MyConnection.Open()
MyConnection.ConnectionTimeout = 500

这么写不对。
supremeholy 2009-02-02
  • 打赏
  • 举报
回复
SqlConnection有ConnectionTimeout属性,用这个。
blueskywjs 2009-02-02
  • 打赏
  • 举报
回复
谢谢各位,不过我用的不是SqlCommand,而是SqlDataAdapter,我看他没有CommandTimeout 属性啊。

Dim daOrders As New Data.SqlClient.SqlDataAdapter(sql, MyConnection)
daOrders.Fill(dsdataSet, "t_biao_cnskb")
lianhui1122 2009-02-02
  • 打赏
  • 举报
回复
查询分析器中执行的时间是3分钟,太长了:
如果你的数据太大,可以用分页,每次返回一部分数据;
尽量不要用 select *,用什么字段写什么字段;
如果非要一次返回,设下 SqlCommand.CommandTimeout
宝_爸 2009-02-02
  • 打赏
  • 举报
回复
SqlCommand.CommandTimeout 可以设置timeout属性
qinhl99 2009-02-02
  • 打赏
  • 举报
回复
不过,还是首先应该优化你的sql啊
qinhl99 2009-02-02
  • 打赏
  • 举报
回复
SqlCommand.CommandTimeout 属性设长一点,比如5分钟
qinhl99 2009-02-02
  • 打赏
  • 举报
回复
比如:
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name = "SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
//SQLString = ClearSQLString(SQLString);
SqlDataAdapter command = new SqlDataAdapter(SQLString,connection);
command.SelectCommand.CommandTimeout = 300;//300s
command.Fill(ds,"ds");
}
catch(System.Data.SqlClient.SqlException ex)
{
//Utility.Logs.WriteLogFile(ex,SQLString);
throw new Exception(ex.Message);
}
finally
{
connection.Close();
connection.Dispose();
}
return ds;
}
}

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧