c#关键字'附近的语法不正确;用户';
c#windows窗体错误; c#关键字“user”附近的语法不正确 请帮帮我,我尝试了所有可能的方法,但都没能解决c#关键字'附近的语法不正确;用户';,c#,C#,c#windows窗体错误; c#关键字“user”附近的语法不正确 请帮帮我,我尝试了所有可能的方法,但都没能解决 private void button1_Click(object sender, EventArgs e) { string fname = textBox1.Text; string lname = textBox2.Text; string username = textBox5.Text; str
private void button1_Click(object sender, EventArgs e)
{
string fname = textBox1.Text;
string lname = textBox2.Text;
string username = textBox5.Text;
string password = textBox6.Text;
string dob = textBox3.Text;
string mobile = textBox4.Text;
string address = richTextBox1.Text;
string gender="";
if(radioButton1.Checked){
gender=radioButton1.Text;
}else if(radioButton2.Checked){
gender=radioButton2.Text;
}else{
MessageBox.Show("Please select Gender");
}
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\users\user\My Documents\visual studio 2012\Projects\cSharpProject\cSharpProject\CSharpProjectDataBase.mdf;Integrated Security=True");
con.Open();
SqlCommand command;
SqlDataAdapter sda = new SqlDataAdapter();
string query = "insert into user values('" + @fname.Trim() + "','" + @lname.Trim() + "','" + @username.Trim() + "','" + @password + "','" + @dob + "','" + @gender + "','" + @mobile.Trim() + "','" + @address + "' ";
command = new SqlCommand(query,con);
sda.InsertCommand = new SqlCommand(query, con);
sda.InsertCommand.ExecuteNonQuery();
}
user
是一个保留字,因此需要转义,如插入[用户]值…
或插入“用户”值…
Microsoft SQL Server使用保留关键字定义、操作和访问数据库。保留关键字是SQL Server用于解析和理解Transact-SQL语句和批处理的Transact-SQL语言语法的一部分。尽管在语法上可以使用SQL Server保留关键字作为Transact-SQL脚本中的标识符和对象名,但您只能通过使用分隔标识符来实现这一点
你可以在这里找到完整的列表
在您的场景中,如果您希望保留您所追求的主题,那么最好将其重命名为“Users”或“UserList”
user
不是保留字吗?检查表名!您应该从查询中的所有变量名中删除@
。阅读关于使用C#和SQL的参数化查询,如果此空键正确,则应使用sqlparameters:-)@“插入到[用户]值中(NULL,“+@fname.Trim()+”),用于自动递增id值的用户Identity
列。