执行datacontext.ExecuteCommand(objectname)
时出错错误:
System.Data.SqlClient.SqlException:超时已过期。操作完成之前经过的超时时间或服务器没有响应。该语句已终止。 示例代码:
Datacontext context = new Datacontext();
tablename tb=new tablename();
string DeleteQuery="delete from table A";
context.ExecuteCommand<tb>(DeleteQuery);
DeleteQuery=string.empty;
string InsertQuery="Insert into table B(a,b,c,d)Select table from C Union All Select Table from D";
context.ExecuteCommand<tb>(InsertQuery);
InsertQuery=string.empty;
这里执行插入查询时遇到错误System.Data.SqlClient.SqlException:超时已过期。操作完成之前经过的超时时间或服务器没有响应。该语句已终止。
错误页面如下
超时已过期。操作完成之前经过的超时时间或服务器没有响应。 该语句已终止。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Data.SqlClient.SqlException:超时已过期。操作完成之前经过的超时时间或服务器没有响应。 声明已经终止。
来源错误:
第914行:其中ft.IsDeleted = 0且ym.IsDeleted = 0且ym.IsApproved = 1&#34;); 915行: 第916行:dc.ExecuteQuery(InsertCommand); 第917行:InsertCommand = string.Empty; 918行:
源文件:d:\ Website \ IDCCircle_Staging \ Portal \ Default.aspx.cs行:916
堆栈追踪:
[SqlException(0x80131904):超时已过期。操作完成之前经过的超时时间或服务器没有响应。 该语句已终止。] System.Data.SqlClient.SqlConnection.OnError(SqlException exception,Boolean breakConnection)+2062078 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception,Boolean breakConnection)+5050204 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()+234 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)+2275 System.Data.SqlClient.SqlDataReader.ConsumeMetaData()+ 33 System.Data.SqlClient.SqlDataReader.get_MetaData()+86 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)+311 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async)+987 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result)+162 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method)+32 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String method)+141 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)+12 System.Data.Common.DbCommand.ExecuteReader()+ 12 System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query,QueryInfo queryInfo,IObjectReaderFactory factory,Object [] parentArgs,Object [] userArgs,ICompiledSubQuery [] subQueries,Object lastResult)+1266 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query,QueryInfo [] queryInfos,IObjectReaderFactory factory,Object [] userArguments,ICompiledSubQuery [] subQueries)+113 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)+344 System.Data.Linq.DataContext.ExecuteMethodCall(Object instance,MethodInfo methodInfo,Object [] parameters)+83 System.Data.Linq.DataContext.ExecuteQuery(String query,Object []参数)+265 _Default.NHPgridbind()在d:\ Website \ IDCCircle_Staging \ Portal \ Default.aspx.cs:916 _Default.Page_Load(Object sender,EventArgs e)位于d:\ Website \ IDCCircle_Staging \ Portal \ Default.aspx.cs:97 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,Object o,Object t,EventArgs e)+14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,EventArgs e)+35 System.Web.UI.Control.OnLoad(EventArgs e)+91 System.Web.UI.Control.LoadRecursive()+ 74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+2207
版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.272
答案 0 :(得分:12)
如果表C和D包含很多行,则运行插入的时间可能比默认命令超时(30秒)要长。在插入命令之前添加此行:
context.CommandTimeout = 240 // set timeout to 4 minutes
顺便说一下,完成后应该处理上下文。最简单的方法是:
using (Datacontext context = new Datacontext())
{
// your code goes here
}