尝试插入访问数据库时,我在INSERT INTO语句中遇到语法错误。发生错误的代码段是
If (checkBox.IsChecked) Then
cmd.CommandText = "INSERT INTO Participants([Full Name],[From],[Gender],[Category],[Event],[Weight],[DOB],[Age]) VALUES(" + textBox.Text + "," + textBox_Copy2.Text + "," + gender + "," + comboBox.SelectedItem.ToString + "," + "Kata" + "," + textBox_Copy.Text + "," + dp.Text + "," + textBox_Copy1.Text
cmd.ExecuteNonQuery()
End If
If (checkBox_Copy2.IsChecked) Then
cmd.CommandText = "INSERT INTO Participants([Full Name],[From],[Gender],[Category],[Event],[Weight],[DOB],[Age]) VALUES(" + textBox.Text + "," + textBox_Copy2.Text + "," + gender + "," + comboBox.SelectedItem.ToString + "," + "Kumite" + "," + textBox_Copy.Text + "," + dp.Text + "," + textBox_Copy1.Text
cmd.ExecuteNonQuery()
End If
If (checkBox_Copy1.IsChecked) Then
cmd.CommandText = "INSERT INTO Participants([Full Name],[From],[Gender],[Category],[Event],[Weight],[DOB],[Age]) VALUES(" + textBox.Text + "," + textBox_Copy2.Text + "," + gender + "," + comboBox.SelectedItem.ToString + "," + "Team Kata" + "," + textBox_Copy.Text + "," + dp.Text + "," + textBox_Copy1.Text
cmd.ExecuteNonQuery()
End If
If (checkBox_Copy.IsChecked) Then
cmd.CommandText = "INSERT INTO Participants([Full Name],[From],[Gender],[Category],[Event],[Weight],[DOB],[Age]) VALUES(" + textBox.Text + "," + textBox_Copy2.Text + "," + gender + "," + comboBox.SelectedItem.ToString + "," + "Team Kumite" + "," + textBox_Copy.Text + "," + dp.Text + "," + textBox_Copy1.Text
cmd.ExecuteNonQuery()
End If
答案 0 :(得分:3)
对于文本字段,您必须将VALUES子句中的值括在引号中。请注意此示例中的单引号
"INSERT INTO Participants([Full Name]) VALUES ('" + textBox.Text + "')"
但是,使用参数而不是将值正确放在SQL语句中会更好。看到这个 https://msdn.microsoft.com/en-us/library/tyy0sz6b(v=vs.110).aspx