tags:

views:

49

answers:

2

Im doing a project and I want to add a record to an accdb(Access databse) using a windows forms application on C# I have got what appears to be code that works i.e. it doesnt show any errors and appears to run however it doesnt add the record, how can I be sure that add this record (image)? Any ideas, code, help would be much appreciated

this is my code

enter code here

    private void button4_Click(object sender, EventArgs e)
    {

        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Title = "Abrir escudo de municipio";

        //openFileDialog1.Filter ="IMG Files|*.jpg|JPG Files|*.png|PNG Files|*.bmp Files|BMP Files|";
        openFileDialog1.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|BITMAPS|*.bmp";
        openFileDialog1.InitialDirectory = @"C:\";


        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {

            // textBox1.Show(openFileDialog1.FileName.ToString());

            //MessageBox.Show(openFileDialog1.FileName.ToString());
            textBox1.Text = openFileDialog1.FileName.ToString();



            String filename = openFileDialog1.FileName.ToString();
            byte[] buffer = File.ReadAllBytes(filename);


            using (var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Policias.accdb"))

            using (var cmd = conn.CreateCommand())
            {

                cmd.CommandText = "INSERT INTO DetallesMunicipio(imagen) VALUES (@imagen)";
                cmd.Parameters.AddWithValue("@imagen", buffer);
                conn.Open();
                cmd.ExecuteNonQuery();



            }
        }
        else
        {
            MessageBox.Show("Please select an image");

        }

    }
A: 

Validate against the return value of cmd.ExecuteNonQuery().

ExecuteNonQuery will return the number of rows affected, in your case the number records INSERTed.

MSDN docs: OleDbCommand.ExecuteNonQuery Method

In the block where you insert your record(s), check the return value on the call to ExecuteNonQuery() against the number of records you expect to insert:

// ...
using (var cmd = conn.CreateCommand()) { 
    cmd.CommandText = "INSERT INTO DetallesMunicipio(imagen) VALUES (@imagen)"; 
    cmd.Parameters.AddWithValue("@imagen", buffer); 
    conn.Open(); 
    int recordsInserted = cmd.ExecuteNonQuery(); 
    if (recordsInserted != 1) {  // if you expect to insert 1 record.
        // Do something because the INSERT failed.
    }
} 
// ...

If you find that this return value is 0, then your INSERT is not working but this doesn't mean there was an error. If there is an error then an Exception will be thrown.

Jay Riggs
but I mean how?
kraven
A: 

Is your parameter getting passed incorrectly? What happens if you try this block of code:

cmd.CommandText = "INSERT INTO DetallesMunicipio(imagen) VALUES (@imagen)";
OleDbParameter imageParameter = cmd.Parameters.Add("@imagen", OleDbType.Binary);
imageParameter.Value = buffer;
imageParameter.Size = buffer.Length;
conn.Open();
cmd.ExecuteNonQuery();
Alex Morris