INSERT INTO语句中的ASP.net语法错误 - cmd.ExecuteNonQuery()

时间:2013-11-17 17:15:15

标签: asp.net

目前正在进行一个项目,我遇到了一些严重的代码问题。我一直在获取cmd.ExecuteNonQuery(),我已经在互联网上搜索到了解决方案。我在某些类别中插入[]仍然会产生相同的错误。

我所做的只是创建一个页面,人们可以在其中输入某些类别的数据,一旦添加,它将更新到访问数据库。我有一个早期版本的工作,但自从我添加更多字符以来,它就出现了这个错误。

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="addcar.aspx.vb" Inherits="WebApplication1.addcar" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
       <p>
        <br />
        CarName<asp:TextBox ID="tb_CarName" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        Year<asp:TextBox ID="tb_Year" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        EngineSize<asp:TextBox ID="tb_EngineSize" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        BHP<asp:TextBox ID="tb_BHP" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        Origin<asp:TextBox ID="tb_Origin" runat="server"></asp:TextBox>
    </p>
      <p>
        <br />
        Description<asp:TextBox ID="tb_Description" runat="server"></asp:TextBox>
    </p>
       <p>
        <br />
        Pictures<asp:TextBox ID="tb_Pictures" runat="server"></asp:TextBox>
    </p>
         <p>
             <asp:Button ID="Button" runat="server" Text="Add Car Details" />
    </p>
   </asp:Content>

后面是按钮代码:

Imports System.Data.OleDb

Public Class addcar
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Protected Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("CarsConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into Table1(CarName,Year,EngineSize,BHP,Origin,Description,Pictures) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_CarName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_Year.Text)
        cmd.Parameters.AddWithValue("@f3", tb_EngineSize.Text)
        cmd.Parameters.AddWithValue("@f4", tb_BHP.Text)
        cmd.Parameters.AddWithValue("@f5", tb_Origin.Text)
        cmd.Parameters.AddWithValue("@f6", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f7", tb_Pictures.Text)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("confirmation.aspx")


    End Sub

End Class

帮助?我做错了什么?

错误如下:

  

INSERT INTO语句中的语法错误。

1 个答案:

答案 0 :(得分:2)

Year是access / Jet中的保留关键字。 你需要用方括号封装它。

Dim SqlString As String = "Insert into Table1(CarName, [Year],EngineSize,BHP,Origin,Description,Pictures) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"

您可以在此检查喷气发动机中的每个保留关键字: http://support.microsoft.com/kb/248738