【问题标题】:SqlException (0x80131904): Line 28: Incorrect syntax near '('.]SqlException (0x80131904): 第 28 行: '('.] 附近的语法不正确
【发布时间】:2010-11-27 16:26:41
【问题描述】:

请有人帮助我! 我有一个使用 sql server 2005 在 windows 2003 上运行的应用程序。当我尝试使用 sql server 2000 在 windows 2003 上的其他服务器上部署这个应用程序时,应用程序的某些页面显示以下消息:

Server Error in '/' Application.
Line 1: Incorrect syntax near '('. 
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: Line 1: Incorrect syntax near '('.

Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 


Stack Trace: 

[SqlException (0x80131904): Line 1: Incorrect syntax near '('.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1948826
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4844747
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
   System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
   System.Data.SqlClient.SqlDataReader.get_MetaData() +83
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
   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(CommandBehavior behavior) +10
   System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +383

[EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.]
   System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +422
   System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +745
   System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +162
   System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +45
   System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +203
   System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1(IEnumerable`1 sequence) +40
   System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +60
   System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +109
   System.Linq.Queryable.FirstOrDefault(IQueryable`1 source) +269
   SebraeFE.Models.Repositories.InterestsRepository.DeleteInterests(Interests interestsToDelete) +418
   SebraeFE.Models.Managers.InterestsManager.DeleteInterests(Interests interestsToDelete) +41
   SebraeFE.Models.Facades.InterestsFacade.DeleteInterests(Interests interestsToDelete) +41
   SebraeFE.Controllers.OportunidadeController.ApagarInteresse(Int32 Id) +265
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +78
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +53
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +258
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +258
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +193
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +382
   System.Web.Mvc.Controller.ExecuteCore() +123
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +144
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +54
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +406
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +76


Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053

下面的技术:

  • asp.net mvc 1

  • ADO.net 和 LINQ

请帮帮我!

【问题讨论】:

  • 如果没有实际查询,将很难解决此问题。
  • 尝试粘贴您的查询而不是堆栈跟踪
  • 从堆栈跟踪来看,在我看来这可能是 MVC 和 SQL2000 的兼容性问题

标签: asp.net sql sql-server linq ado.net


【解决方案1】:

用 xml 编辑器打开你的模型 .edmx,然后修改它

 ProviderManifestToken="2008"

 ProviderManifestToken="2000"

它可能是生成在 sql2005 中工作的 sql 脚本“Top (1)”的 linq .first(),但在 sql 2000 中应该是“top 1”而没有 '('and ')'

它适用于我的解决方案。

【讨论】:

  • 我也有同样的问题,但它在“-”附近给出了不正确的语法。而不是“'('附近的语法不正确。”我会做什么
【解决方案2】:

在 .edmx 文件中,您需要将 Schema 标记中的 ProviderManifestToken 更改为 2000 为 ProviderManifestToken="2000"(它将是 ProviderManifestToken="2005")

参考:Entity Framework on Sql 2000 vs. Sql 2005 and ProviderManifestToken

【讨论】:

    【解决方案3】:

    一些 SQL Server 2005 查询包含特定于 2005 的扩展,例如 ROW_NUMBER()、公用表表达式 [e.g. WITH x AS (...) SELECT ... FROM x] 等与 SQL Server 2000 不向后兼容。

    您必须确保您的应用程序仅在 SQL Server 2005 上运行,或者您必须重新处理特定于 SQL Server 2005 的查询,以支持 SQL Server 2000、2005 和可能的 2008,以达到良好的效果。关于不同版本的 SQL Server 在 T-SQL 语法和语义方面的差异,有大量文档可供使用。

    如果不透露实际的查询文本,恐怕您不会从任何人那里获得更具体的帮助。这就像我要求你纠正我在第三年英语学期论文第 12 行的拼写错误。不,你不能拥有它,但你能帮我校对吗?

    【讨论】:

      【解决方案4】:

      如果不知道您的查询是什么样的,这是非常困难的。这个特殊的例外让我以前使用 SELECT TOP 表达式。在 SQL 2005+ 中,一个简单的 SELECT TOP 可能如下所示:

      SELECT TOP (50) FROM Events
      

      但是,您将收到您在 SQL 2000 中所说的确切语法错误异常,因为 SQL 2000 不允许在 SELECT TOP 表达式中使用括号。换句话说,它需要看起来像这样:

      SELECT TOP 50 FROM Events
      

      另一个尝试深入了解的技巧是获取生成的确切 SQL 语句,然后进入 SQL 2000 企业管理器并运行它。在某些情况下,当它给你同样的错误时,EM 会让你更接近语法问题存在的位置(尝试将你的查询分成多行)

      【讨论】:

        【解决方案5】:

        这个错误刚刚发生在我身上,在 .NET 框架 4 上运行的 Windows 服务中使用带有 SQL Server 2008 的实体框架。

        真的很简单,仔细检查您的查询在 linq 中是类型安全的。如果fldvarchar,则IE 不要执行fld &gt; 0 之类的where 子句,你应该执行fld isnot nothing。我认为的问题是当 .net 尝试将 linq 类型与 db 字段类型匹配时,SQL Server 不喜欢运行生成的 SQL。

        所以基本上这个错误是野餐而不是软件故障。

        我的查询失败了

        qry = qry.Where(Function(rslts) (rslts.SubscriptionEndDate <= DeadLine _
               And rslts.SubscriptionCancelledByName > 0 _
               And rslts.SubscriptionIsRenewalEmailSent = False))
        

        解决问题的查询是

        qry = qry.Where(Function(rslts) (rslts.SubscriptionEndDate <= DeadLine _
               And rslts.SubscriptionCancelledByName IsNot Nothing _
               And rslts.SubscriptionIsRenewalEmailSent = False))
        

        希望对你有帮助

        【讨论】:

          【解决方案6】:

          听起来很可能您正在尝试使用 SQL 2005 具有而 SQL 2000 没有的东西。常用表表达式?

          【讨论】:

            【解决方案7】:

            可能是 LINQ。这不是 100% compatible with Sql 2000

            【讨论】:

              【解决方案8】:

              我没有找到真正的原因。但这似乎与 linq 和 sql2000 有关,正如 Charles Conway 所说。

              下面的代码对我有用:

              替换:

              //...
                  Interests original = (from m in _db.InterestsSet where m.Id == interestsToDelete.Id select m).FirstOrDefault();
              //...
              

              到:

              //..
                      Interests original = null;
              
                      foreach (var i in from m in _db.InterestsSet where m.Id == interestsToDelete.Id select m){
                          original = i;
                          break;
                      }
                  //...
              

              我不喜欢它,但是可以...

              【讨论】:

                猜你喜欢
                • 2014-12-18
                • 2018-06-28
                • 2020-05-14
                • 1970-01-01
                • 1970-01-01
                • 2014-11-09
                • 1970-01-01
                • 2021-01-23
                • 2011-05-28
                相关资源
                最近更新 更多