Visual Studios 2013在关键字';表';
本文关键字:关键字 Studios 2013 Visual | 更新日期: 2023-09-27 18:20:36
我正在使用Visual studio 2013构建一个网站,我一直在让用户注册,一旦提交,详细信息就会存储在数据库中,经理可以登录并查看所有注册的帐户。但是我得到了
Error:System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'Table'. at System.Data.SqlClient.SqlConnection.
BTW表是我将注册帐户数据存储到的数据库表的名称。
任何帮助都是挪用的!
这是提交按钮的代码
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;
using System.Configuration;
public partial class Registration :
{
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString-reg"].ConnectionString); //connects to regstration DB//
conn.Open(); //to open connection to DB
String checkuser = "select count(*) from Table where Name= '"+ nameBox.Text +"'"; // checks wether the name entered is in the database already //
SqlCommand com = new SqlCommand(checkuser, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("User already exists");
}
conn.Close(); //to close connection to DB after executing a query//
}
catch (SqlException Ex)
{
Response.Write("Error:" + Ex.ToString());
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString-reg"].ConnectionString); //connects to regstration DB//
conn.Open(); //to open connection to DB
String insertQuery = "insert into Table (Name, Date of birth, Yoga experience, Health issues, Email address, Telephone number, Password) values (@Name, @dob, @exp, @health, @email, @phone, @password)"; // inserts into table and declares data as variables//
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Name", nameBox.Text);
com.Parameters.AddWithValue("@dob", Yeardropdown.SelectedItem.ToString());
com.Parameters.AddWithValue("@expe", expBox.Text);
com.Parameters.AddWithValue("@health", healthBox.Text);
com.Parameters.AddWithValue("@email", emailBox.Text);
com.Parameters.AddWithValue("@phone", phoneBox.Text);
com.Parameters.AddWithValue("@passwrd", passwrdBox.Text);
com.ExecuteNonQuery();
Response.Redirect("Manager.aspx");
Response.Write("Thank you, Your registration has been successfull!");
conn.Close();
}
catch (SqlException ex)
{
Response.Write("Error:" + ex.ToString());
}
}
protected void Daydropdown_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
两件事:
1) 您使用了table作为表名。由于它已经是SQL中的一个关键字,所以请更改此名称。尽管您可以使用以下命令:
Insert into [Table]....
2) 您的列名包含空格,因此可能会导致一些错误。如果列名中包含空格,请在列名周围使用括号。
String insertQuery = "insert into Table (Name, [Date of birth], [Yoga experience], [Health issues], [Email address], [Telephone number], Password) values (@Name, @dob, @exp, @health, @email, @phone, @password)";
我们不能在sqlserver中使用table作为表名,更改您的表名,它会起作用,并且不要忘记在查询中也更改您的表格名。
String insertQuery = "insert into changethistablename (Name, Date of birth, Yoga experience, Health issues, Email address, Telephone number, Password) values (@Name, @dob, @exp, @health, @email, @phone, @password)"; // inserts into table and declares data as variables//
String checkuser = "select count(*) from changethistablename where Name= '"+ nameBox.Text +"'"; // checks wether the name entered is in the database already //
另一件事是为什么需要检查IsPostBack
?
SQL
中的第一个Table
将是Table
语法。
如果必须使用语法作为名称
Microsoft SQL Server
将SQL查询从table
修改为[table]
它应该起作用。。
您是否在SQL Management Studio中运行了代码
我相信,如果您使用该查询尝试insert
或select
,您会遇到错误
语法为名称您必须添加[]