BD ACCESS does not accept INSERT or UPDATE

1

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

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.");
        }
    
asked by anonymous 23.06.2015 / 07:15

2 answers

1

Try something like this:

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");
        }
    
23.06.2015 / 14:45
1

Try this:

cmd.Parameters.AddWithValue("@nome", textBox1.Text);
    
23.06.2015 / 10:36