【发布时间】: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());
}
}
}
}
【问题讨论】: