当我点击注册按钮将信息插入MS Access时,我得到了这个错误
INTO语句中的错误
我的代码:
private void btnRegister_Click(object sender, EventArgs e)
{
string userId = userIdTextBox.Texts;
string password = passwordTextBox.Texts;
string surname = surnameTextBox.Texts;
string givenName = givenNameTextBox.Texts;
string course = courseTextBox.Texts;
string section = sectionTextBox.Texts;
string yearLevel = yearLevelTextBox.Texts;
int userType = (int)userTypeComboBox.SelectedIndex; // Get the selected user type ID
if (userType == 1 && userType == 2 && userIdTextBox.Texts == "" || passwordTextBox.Texts == "" || surnameTextBox.Texts == "" || givenNameTextBox.Texts == "" || courseTextBox.Texts == "")
{
MessageBox.Show("Required Missing Field", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (userType == 1 && userId.Contains("MA") && userId.Contains("ma"))
{
MessageBox.Show("Students cannot register as instructors or system admins.");
userTypeComboBox.SelectedIndex = -1; // Clear selection
return;
}
}
try
{
connection.Open();
cmd = new OleDbCommand ("INSERT INTO tblUser ([User Id], [Password], [Surname], [Given Name], [Course], [Section], [Year/Level], [User Type])" +
"VALUES ([@User Id], @Password, @Surname, [@Given Name], @Course, [@Section], [@Year/Level], [@User Type]", connection);
cmd.Parameters.AddWithValue("@User Type", userTypeComboBox.SelectedIndex);
cmd.Parameters.AddWithValue("@User Id", userIdTextBox.Texts);
cmd.Parameters.AddWithValue("@Password", passwordTextBox.Texts);
cmd.Parameters.AddWithValue("@Surname", surnameTextBox.Texts);
cmd.Parameters.AddWithValue("@Given Name", givenNameTextBox.Texts);
cmd.Parameters.AddWithValue("@Course", courseTextBox.Texts);
cmd.Parameters.AddWithValue("@Section", sectionTextBox.Texts);
cmd.Parameters.AddWithValue("@Year/Level",yearLevelTextBox.Texts);
cmd.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
connection.Close();
MessageBox.Show(ex.Message, "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
我已经把括号,但为什么它仍然不能正常工作。
1条答案
按热度按时间wvt8vs2t1#
你的代码中有两个主要问题:
(1)SQL
INSERT
语句缺少结束)
paren(2)OleDB/MS Access中的参数是位置-例如:匹配SQL语句中参数的位置,以及
OleDbCommand
的参数声明。他们的名字无关紧要。试试这个代码: