我正在尝试设置我的数据库以使用我的应用程序。但每当我尝试将数据插入数据库时,我都会收到此错误。
System.Data.SqlClient.SqlException
Message =关键字“Function”附近的语法不正确。 Source = .Net SqlClient数据提供程序
private void insertBtn_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into dbo.Users" + "(Firstname, MiddleName, Lastname,DateofBirth, Nationality, Tshirtsize, Poloshirtsize, Jacketsize, Pantssize, Shoessize, Street, Street2, City, ZipCode, Country, Homephonenumber, Mobilephonenumber, Alternativemobilephonenumber, Workphonenumber, Faxnumber, Emailaddress, Alternativeemailaddress, Skypecontactinformation, Facebooknameorprofilelink, InternationalAirport, DomesticAirportNearest, Function, Secondfunction, Level, Zone, Examinationdate, Examinationlocation, Examiner1, Examiner2)values(@Firstname,@Lastname,@DateofBirth, @Nationality, @Tshirtsize, @Poloshirtsize, @Jacketsize, @Pantssize, @Shoessize, @Street, @Street2, @City, @ZipCode, Country, @Homephonenumber, @Mobilephonenumber,@Alternativemobilephonenumber, @Workphonenumber, @Faxnumber, @Emailaddress, @Alternativeemailaddress, @Skypecontactinformation, @Facebooknameorprofilelink, @InternationalAirport, @DomesticAirportNearest, @Function, @Secondfunction, @Level, Zone, @Examinationdate, @Examinationlocation, @Examiner1, @Examiner2)", conn);
cmd.Parameters.AddWithValue("@Firstname", firstname.Text);
cmd.Parameters.AddWithValue("@Lastname", lastname.Text);
cmd.Parameters.AddWithValue("@MiddleName", middlename.Text);
cmd.Parameters.AddWithValue("@DateofBirth", bunifuDatePickerOfBirth.Text);
cmd.Parameters.AddWithValue("@Nationality", nationality.Text);
cmd.Parameters.AddWithValue("@Tshirtsize", tshirtSize.Text);
cmd.Parameters.AddWithValue("@Poloshirtsize", poloshirts.Text);
cmd.Parameters.AddWithValue("@Jacketsize", jacketSize.Text);
cmd.Parameters.AddWithValue("@Pantssize", pantsSize.Text);
cmd.Parameters.AddWithValue("@Shoessize", shoesSize.Text);
cmd.Parameters.AddWithValue("@Street", street.Text);
cmd.Parameters.AddWithValue("@Street2", street2.Text);
cmd.Parameters.AddWithValue("@City", city.Text);
cmd.Parameters.AddWithValue("@ZipCode", zip.Text);
cmd.Parameters.AddWithValue("@Country", country.Text);
cmd.Parameters.AddWithValue("@Homephonenumber", homePhone.Text);
cmd.Parameters.AddWithValue("@Mobilephonenumber", mobilePhone.Text);
cmd.Parameters.AddWithValue("@Alternativemobilephonenumber", alterMobile.Text);
cmd.Parameters.AddWithValue("@Workphonenumber", workPhone.Text);
cmd.Parameters.AddWithValue("@Faxnumber", faxNum.Text);
cmd.Parameters.AddWithValue("@Emailaddress", email.Text);
cmd.Parameters.AddWithValue("@Alternativeemailaddress", alterEmail.Text);
cmd.Parameters.AddWithValue("@Skypecontactinformation", skypeContact.Text);
cmd.Parameters.AddWithValue("@Facebooknameorprofilelink", facebook.Text);
cmd.Parameters.AddWithValue("@InternationalAirport", InternationalAirport.Text);
cmd.Parameters.AddWithValue("@DomesticAirportNearest", DomesticAirport.Text);
cmd.Parameters.AddWithValue("@Function", function.Text);
cmd.Parameters.AddWithValue("@Secondfunction", secondFun.Text);
cmd.Parameters.AddWithValue("@Level", Level.Text);
cmd.Parameters.AddWithValue("@Zone", Zone.Text);
cmd.Parameters.AddWithValue("@Examinationdate", examDate.Text);
cmd.Parameters.AddWithValue("@Examinationlocation", examLocation.Text);
cmd.Parameters.AddWithValue("@Examiner1", examiner1.Text);
cmd.Parameters.AddWithValue("@Examiner2", examiner2.Text);
cmd.ExecuteNonQuery();
conn.Close();
firstname.Text = "";
middlename.Text = "";
lastname.Text = "";
street.Text = "";
street2.Text = "";
city.Text = "";
zip.Text = "";
homePhone.Text = "";
mobilePhone.Text = "";
alterMobile.Text = "";
skypeContact.Text = "";
facebook.Text = "";
InternationalAirport.Text = "";
examDate.Text = "";
examiner1.Text = "";
examiner2.Text = "";
displayData();
}
我试着把这些单词写在一起,因为我认为它们导致了错误。
DomesticAirportNearest, --> Function, Secondfunction,
function是保留关键字。
如果你正在使用MySql,你需要使用返回tick来逃避它
DomesticAirportNearest, `Function`, Secondfunction,
但由于异常说System.Data.SqlClient.SqlException所以我假设它的Sql Server。如果它的Sql server需要使用[]
DomesticAirportNearest, [Function], Secondfunction,
尝试更改单词function
in这一行:cmd.Parameters.AddWithValue("@Function", function.Text);