C# Oledb 0x80040E14 INSERT INTO error

2019-08-31 06:54发布

问题:

I'm working on a project where you can add users to a database with the INSERT INTO command and an Acces database (OleDB).

I'm having an 0x80040E14 error which I can't seem to get solved. Code below:

public void AddUser()
{
    OleDbConnection DATABASE_CONNECTION = new OleDbConnection(CONNECTION_STRING);

    try
    {
        DATABASE_CONNECTION.Open();
        String SQL_QUERY = "INSERT INTO Users(Username, Password, Voornaam, Tussenvoegsel, Achternaam, Email, VolledigeNaam) VALUES(?, ?, ?, ?, ?, ?, ?)";
        OleDbCommand COMMAND = new OleDbCommand(SQL_QUERY, DATABASE_CONNECTION);

        COMMAND.Parameters.AddWithValue("Username", OleDbType.Char).Value = Username;
        COMMAND.Parameters.AddWithValue("Password", OleDbType.Char).Value = Password;
        COMMAND.Parameters.AddWithValue("Voornaam", OleDbType.Char).Value = Voornaam;
        COMMAND.Parameters.AddWithValue("Tussenvoegsel", OleDbType.Char).Value = Tussenvoegsel;
        COMMAND.Parameters.AddWithValue("Achternaam", OleDbType.Char).Value = Achternaam;
        COMMAND.Parameters.AddWithValue("Email", OleDbType.Char).Value = Email;
        COMMAND.Parameters.AddWithValue("VolledigeNaam", OleDbType.Char).Value = VolledigeNaam;

        int rows = COMMAND.ExecuteNonQuery();
        DATABASE_CONNECTION.Close();

        if (rows > 0)
        {
            MessageBox.Show("User added");
        }
        else
        {
            MessageBox.Show("FAIL");
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

回答1:

Password is a reserved keyword in MSAccess. To use it you need to enclose it in square brackets

   String SQL_QUERY = "INSERT INTO Users(Username, [Password], Voornaam, Tussenvoegsel, " + 
                      "Achternaam, Email, VolledigeNaam) VALUES(?, ?, ?, ?, ?, ?, ?)";

However, if it is still possible, change the column name because this problem will be very annoying.

There is another error in your code.

The method AddWithValue requires, as second parameter, the value for of the OleDbParameter, not the type.

  COMMAND.Parameters.AddWithValue("Username", Username);
  ....


标签: c# insert oledb