winforms 我得到一个错误“INTO语句中的错误”

3pvhb19x  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(106)

当我点击注册按钮将信息插入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);
    }
}

我已经把括号,但为什么它仍然不能正常工作。

wvt8vs2t

wvt8vs2t1#

你的代码中有两个主要问题:

(1)SQL INSERT语句缺少结束)paren
(2)OleDB/MS Access中的参数是位置-例如:匹配SQL语句中参数的
位置
,以及OleDbCommand的参数声明。他们的名字无关紧要。

试试这个代码:

private void btnRegister_Click(object sender, EventArgs e)
{
    // removed OK parts of method at the beginning ....

    try
    {
        connection.Open();

        // changed parameters names - those are totally irrelevant
        cmd = new OleDbCommand ("INSERT INTO tblUser ([User Id], [Password], [Surname], [Given Name], [Course], [Section], [Year/Level], [User Type])" +
                                "VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8)", connection);
        //                                   here is a MISSING closing paren  ***

        // Parameters **MUST BE** specified in the same order as listed in the SQL statement
        cmd.Parameters.AddWithValue("@p1", userIdTextBox.Texts);
        cmd.Parameters.AddWithValue("@p2", passwordTextBox.Texts);
        cmd.Parameters.AddWithValue("@p3", surnameTextBox.Texts);
        cmd.Parameters.AddWithValue("@p4", givenNameTextBox.Texts);
        cmd.Parameters.AddWithValue("@p5", courseTextBox.Texts);
        cmd.Parameters.AddWithValue("@p6", sectionTextBox.Texts);
        cmd.Parameters.AddWithValue("@p7",yearLevelTextBox.Texts);
        cmd.Parameters.AddWithValue("@p8", userTypeComboBox.SelectedIndex);

        cmd.ExecuteNonQuery();

        connection.Close();
    }
    catch (Exception ex)
    {
        connection.Close();
        MessageBox.Show(ex.Message, "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}

相关问题