我是c#的初学者,我正在尝试地狱,但我找不到它的错误。我使用过[user]因为user是一个关键字而没有[]。
就无法使用 public partial class NewUser : Form
{
SqlConnection con;
string constr = "Data Source=CHANDAN;Initial Catalog=Airline;Integrated Security=True;";
public NewUser()
{
con = new SqlConnection(constr);
InitializeComponent();
}
private void buttonRegister_Click(object sender, EventArgs e)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("Insert into [User] (Email,password,FirstName,LastName,Age,Gender) values(@Email,@password,@FirstName,@LastName,@Age,@Gender", con);
cmd.Parameters.Add("Email", SqlDbType.VarChar);
cmd.Parameters["Email"].Value = this.TextBoxEmail.Text;
cmd.Parameters.Add("password", SqlDbType.VarChar);
cmd.Parameters["password"].Value = (this.TextBoxPassword.Text);
cmd.Parameters.Add("FirstName", SqlDbType.VarChar);
cmd.Parameters["FirstName"].Value = (this.TextBoxFirstName.Text);
cmd.Parameters.Add("LastName", SqlDbType.VarChar);
cmd.Parameters["LastName"].Value = (this.TextBoxLastName.Text);
cmd.Parameters.Add("Age", SqlDbType.Int);
cmd.Parameters["Age"].Value = Convert.ToInt32(this.TextBoxAge.Text);
cmd.Parameters.Add("Gender", SqlDbType.VarChar);
cmd.Parameters["Gender"].Value = (this.ComboBoxGender.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Inserted Sucessfully");
con.Close();
}
catch (Exception e1)
{
MessageBox.Show("Exception Occured" + "\n" + e1.ToString());
}
}
答案 0 :(得分:1)
最后缺少右括号
Insert into [User] (Email,password,FirstName,LastName,Age,Gender)
values(@Email,@password,@FirstName,@LastName,@Age,@Gender) --here
注意:最好不要将 reserved keywords (USER
)用作标识符