BD ACCESS does not accept INSERT or UPDATE

Asked

Viewed 329 times

1

I have a solution in C# with access database that accepts SELECT, but does not accept INSERT or UPDATE, Follow code of the insert button:

OleDbConnection con = new OleDbConnection();

con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|/teste.accdb";

        try
        {
            con.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "INSERT INTO Tabela1 (Nome) VALUES ( @nome )";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@nome", OleDbType.VarChar, 50).Value = textBox1.Text;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            MessageBox.Show("Inclusão efetuada com sucesso!");
            con.Close();
        }
        catch(Exception)
        {
            MessageBox.Show("Falha na inserção de registro.");
        }
  • Try to put it as code, otherwise nobody understands...

  • @Ivanteles Could post the error generated? Already found the answer?

  • No error generates Ivan, it runs normal, gives confirmation, but does not insert in the bank

2 answers

1

Try it this way:

cmd.Parameters.AddWithValue("@nome", textBox1.Text);
  • Peter continued not working

  • Make sure the file is not Read Only...

  • how do I see it Fernando?

  • is not only reading Fernando

1


Test something like that:

OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|/teste.accdb";
        conn.Open();
        string Name = textBox1.Text;
        OleDbCommand cmmd = new OleDbCommand("INSERT INTO Tabela1 (Nome) Values(@nome)", conn);
        if (conn.State == ConnectionState.Open)
        {
            cmmd.Parameters.Add("@nome", OleDbType.VarWChar, 20).Value = Name;
            try
            {
                cmmd.ExecuteNonQuery();
                MessageBox.Show("Inclusão efetuada com sucesso!");
                conn.Close();
            }
            catch (OleDbException expe)
            {
                MessageBox.Show(expe.Message);
                conn.Close();
            }
        }
        else
        {
            MessageBox.Show("Falha na inserção de registro");
        }
  • Didn’t work Ivan

  • From error? Happens what? Already debugged?

  • No errors. Runs normally, only does not insert in the bank

Browser other questions tagged

You are not signed in. Login or sign up in order to post.