lerrorSystem.Data.SqlClient.SqlException(0x80131904):关键字' User'附近的语法不正确

时间:2017-11-03 10:21:50

标签: sql asp.net

我正在尝试将数据从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());
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

User是一个保留关键字,因此您必须使用方括号来明确表示您指的是名为" User" 的对象,即使用 [用户] 而不是用户。