如何防止这种dling / sql错误 - 或者是因为糟糕的网络?

时间:2012-02-22 08:28:17

标签: linq sql-server-2008 dynamic-linq

第1步) 我检查我可以访问服务器: (因为我有离线处理)

public static bool dbOnline(string timeOut="3")
{
  bool online = false;
       try
       {
           var testConn = new SqlConnection(Utils.ConnectionStr(timeOut)); 
           testConn.Open();
           online = true;
           testConn.Close();
       }
       catch (Exception)
       {
           online = false;
       }
       return online;
   }

2)它返回ok,然后我创建dlinq实例!

_DataContext = new DataContext(Utils.ConnectionStr);//System.Data.Linq.DataContext
//then i point out a customer in DB

var customer = _DataContext.Customers.TableQuery().FirstOrDefault(c => c.ID == custid);

//Check its not null
if (customer != null)
{
            customer.points+= _points;
            customer.LastUpdated = DateTime.Now;
            customer.SubmitChanges();
}

3)有时我会得到这个错误,我能做些什么来阻止这个,或者是它 修复网络? (我知道网络存在一些问题),但我可以做任何事情 不同? - 谢谢

错误: * System.Data.SqlClient.SqlException(0x80131904):超时已过期。操作完成之前经过的超时时间或服务器没有响应。 *

The statement has been terminated.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicUpdate(TrackedObject item)
   at System.Data.Linq.ChangeDirector.StandardChangeDirector.Update(TrackedObject item)
   at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)

1 个答案:

答案 0 :(得分:1)

超时无法单独识别,但我通常会认为网络错误会产生不同的错误。我首先要看的东西:

  • 是正确索引的数据
  • 当时有阻塞,很可能是一个长期运行的事件,它锁定了数据
  • 当时正在发生数据库增长
  • 当时服务器异常繁忙

我的钱将在第二个

另外,我们看不到TableQuery()是什么;如果它返回IEnumerable<Customer>,那么它可能与拉过太多数据有关(在你的上下文中,它可能应该在最小值处返回IQueryable<Customer>,但我们看不到)。

这里要做的真正的事情是运行SQL跟踪并尝试在行为中捕获它 - 看看发生了什么。