关键字“Key”附近的语法不正确。 ASP.net 详细信息视图中的错误

Posted

技术标签:

【中文标题】关键字“Key”附近的语法不正确。 ASP.net 详细信息视图中的错误【英文标题】:Incorrect syntax near the keyword 'Key'. Error in ASP.net DetailsView 【发布时间】:2021-07-16 06:08:06 【问题描述】:

我用 asp.net 设置了一个 DetailsView。我启用了编辑、插入和删除。在我最终提交时的编辑和插入选项中,我得到了这个:

关键字“Key”附近的语法不正确。

堆栈跟踪为:

[SqlException (0x80131904): 关键字'Key'附近的语法不正确。]

System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔型 breakConnection,Action1 wrapCloseInAction) +2582782 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +6033430 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +297 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4291 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted) +262 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 超时,Task& 任务,Boolean asyncWrite,Boolean inRetry,SqlDataReader ds,Boolean describeParameterEncryptionRequest)+2698 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +1611 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +390 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +297 System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand命令,DataSourceOperation操作)+400 System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary 值)+419 System.Web.UI.DataSourceView.Insert(IDictionary 值,DataSourceViewOperationCallback 回调)+100 System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean CauseValidation) +380 System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean CauseValidation, String validationGroup) +647 System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +92 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +88 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkBut​​ton.OnCommand(CommandEventArgs e) +127 System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(String eventArgument) +168 System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9858668 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696

它自动生成的代码是这样的:

<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"  
     AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="UserDataSet" 
     Height="50px" Width="125px">
    <Fields>
        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
        <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" />
        <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
        <asp:CheckBoxField DataField="Admin" HeaderText="Admin" SortExpression="Admin" />
        <asp:BoundField DataField="Story Key" HeaderText="Story Key" SortExpression="Story Key" />
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="UserDataSet" runat="server" 
     ConnectionString="<%$ ConnectionStrings:UserDBConnectionString %>" 
     DeleteCommand="DELETE FROM [UserTable] WHERE [Id] = @Id" 
     InsertCommand="INSERT INTO [UserTable] ([Id], [Username], [Password], [Admin], [Story Key]) VALUES (@Id, @Username, @Password, @Admin, @Story_Key)" 
     SelectCommand="SELECT * FROM [UserTable]" 
     UpdateCommand="UPDATE [UserTable] SET [Username] = @Username, [Password] = @Password, [Admin] = @Admin, [Story Key] = @Story_Key WHERE [Id] = @Id">
     <DeleteParameters>
         <asp:Parameter Name="Id" Type="Int32" />
     </DeleteParameters>
     <InsertParameters>
         <asp:Parameter Name="Id" Type="Int32" />
         <asp:Parameter Name="Username" Type="String" />
         <asp:Parameter Name="Password" Type="String" />
         <asp:Parameter Name="Admin" Type="Boolean" />
         <asp:Parameter Name="Story_Key" Type="String" />
     </InsertParameters>
     <UpdateParameters>
         <asp:Parameter Name="Username" Type="String" />
         <asp:Parameter Name="Password" Type="String" />
         <asp:Parameter Name="Admin" Type="Boolean" />
         <asp:Parameter Name="Story_Key" Type="String" />
         <asp:Parameter Name="Id" Type="Int32" />
     </UpdateParameters>
</asp:SqlDataSource>

我不知道问题是什么,也找不到我在其他问题中寻找的内容。我刚刚使用了来自 asp.net 的自动工具和一个网络表单。我正在遵循 Youtube 教程中的指南。

【问题讨论】:

【参考方案1】:

您的列名(故事键)包含一个“空格”字符,请参考本文https://docs.microsoft.com/en-us/sql/odbc/microsoft/column-name-limitations?view=sql-server-ver15 以获得解决方案。

【讨论】:

以上是关于关键字“Key”附近的语法不正确。 ASP.net 详细信息视图中的错误的主要内容,如果未能解决你的问题,请参考以下文章

System.Data.SqlClient.SqlException:关键字“FROM”附近的语法不正确

关键字“FOR”XML 附近的语法不正确

错误“关键字 'CONVERT' 附近的语法不正确”

关键字“read”附近的语法不正确

关键字“或”附近的语法不正确

在关键字“pivot”附近出现语法错误,提示语法不正确。我在这里想念啥