【问题标题】:lerrorSystem.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'User'lerrorSystem.Data.SqlClient.SqlException (0x80131904):关键字“用户”附近的语法不正确
【发布时间】:2017-11-03 10:21:50
【问题描述】:

我正在尝试将数据从 aspx 页面插入数据库,但不断收到错误消息:

errorSystem.Data.SqlClient.SqlException (0x80131904):关键字“用户”附近的语法不正确

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace DatabaseConnectivity
{
    public partial class Registration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegiConnectionString"].ConnectionString);
                conn.Open();
                string insertQuery = ("insert into User(firstname,secondname,username,country,console,email,gamertag,password)values (@firstname,@secondname,@username,@country,@console,@email,@gamertag,@password)");
                SqlCommand cmd = new SqlCommand(insertQuery, conn);
                cmd.Parameters.AddWithValue("@firstname", TextBox1.Text);
                cmd.Parameters.AddWithValue("@secondname", TextBox2.Text);
                cmd.Parameters.AddWithValue("@username", TextBox3.Text);
                cmd.Parameters.AddWithValue("@country", TextBox4.Text);
                cmd.Parameters.AddWithValue("@console", TextBox5.Text);
                cmd.Parameters.AddWithValue("@email", TextBox6.Text);
                cmd.Parameters.AddWithValue("@gamertag", TextBox7.Text);
                cmd.Parameters.AddWithValue("@password", TextBox8.Text);
                cmd.ExecuteNonQuery();

                Response.Write("User Successfully Registered");

                conn.Close();
            }
            catch(Exception ex)
            {
                Response.Write("error" + ex.ToString());
            }
        }
    }
}

【问题讨论】:

标签: sql asp.net


【解决方案1】:

User 是保留关键字,所以必须使用方括号明确表示您的意思是名为"User"的对象,即使用[User] 而不是用户。

【讨论】:

    猜你喜欢
    • 2014-12-18
    • 2018-06-28
    • 2020-05-14
    • 1970-01-01
    • 2016-06-08
    • 2013-12-16
    • 2017-11-22
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多