1

我使用 EntityFrameWork v5.0,EntityDataSource 控件。

在代码中我设置了 SQL 查询,但是当我在编译后打开页面时出现异常。我该如何解决?

代码:

entryListDs.CommandParameters.Add(new Parameter("subId", DbType.Int32, Common.CurrentUserID.ToString()));
entryListDs.CommandParameters.Add(new Parameter("typeId", DbType.Int32, ((int)MessageType.Wall).ToString()));

entryListDs.CommandText = @"SELECT 
m.ID,
m.To,
m.Subject,
m.BodyHtml,
m.CreationDate,
m.New,
m.TypeID
FROM 
message AS m
JOIN subscription AS s
ON m.From = s.ObjectID
WHERE s.SubscriberID = @subId and m.TypeID = @typeId
ORDER BY m.CreationDate DESC";

打开页面时出现异常:

'From' 是保留关键字,不能用作别名,除非它被转义。在第 12 行第 6 列附近。

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.EntitySqlException: 'From' is a reserved keyword and cannot be used as an alias, unless it is escaped. Near line 12, column 6.

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:

[EntitySqlException: 'From' is a reserved keyword and cannot be used as an alias, unless it is escaped. Near line 12, column 6.]
   System.Data.Common.EntitySql.CqlLexer.MapUnescapedIdentifier(String symbol) +8073331
   System.Data.Common.EntitySql.CqlLexer.MapIdentifierOrKeyword(String symbol) +57
   System.Data.Common.EntitySql.CqlLexer.Accept_83() +36
   System.Data.Common.EntitySql.CqlLexer.yylex() +386
   System.Data.Common.EntitySql.CqlParser.yylex() +16
   System.Data.Common.EntitySql.CqlParser.yyparse() +132
   System.Data.Common.EntitySql.CqlParser.internalParseEntryPoint() +101
   System.Data.Common.EntitySql.CqlParser.Parse(String query) +148
   System.Data.Common.EntitySql.CqlQuery.Parse(String commandText, ParserOptions parserOptions) +41
   System.Data.Common.EntitySql.CqlQuery.CompileCommon(String commandText, Perspective perspective, ParserOptions parserOptions, Func`3 compilationFunction) +84
   System.Data.Common.EntitySql.CqlQuery.CompileQueryCommandLambda(String queryCommandText, Perspective perspective, ParserOptions parserOptions, IEnumerable`1 parameters, IEnumerable`1 variables) +100
   System.Data.Objects.EntitySqlQueryState.Parse() +8608663
   System.Data.Objects.EntitySqlQueryState.GetResultType() +4
   System.Data.Objects.ObjectQuery.GetResultType() +58
   System.Web.UI.WebControls.EntityDataSourceQueryBuilder`1.BuildBasicQuery(ObjectContext context, Boolean computeCount) +108
   System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelectTyped(DataSourceSelectArguments arguments, Creator qbConstructor) +355
   System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +606
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +138
   System.Web.UI.WebControls.ListView.PerformSelect() +102
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105
   System.Web.UI.WebControls.ListView.CreateChildControls() +52
   System.Web.UI.Control.EnsureChildControls() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +42
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18033 
4

2 回答 2

0

它在您的 Exception: 中说From is a reserved keyword

在您的查询中,您有ON m.From = s.ObjectID. 您需要将From列重命名为其他名称。

于 2013-04-20T09:07:12.443 回答
0

如果在列名中使用关键字或特殊符号,则需要括号。只需将列名放在方括号中:

ON m.[From] = s.ObjectID
于 2013-04-20T09:24:32.480 回答