tags:

views:

51

answers:

3

I am developing a C# application that has an Access database. What I want to do is allow a user to select an image through an "openfiledialog." I then want to store the image in one of the table of the access database in a BLOB field. I have searched over the internet, but found nothing helpful. I hope you can help me.

enter code here

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("Porfavor selecciona una imagen");

        }

    }

but now how can I be sure that is stored in the access database?

A: 

What you will want to do is something similar to the following.

using (OpenFileDialog fileDialog = new OpenFileDialog){
   if(fileDialog.ShowDialog == DialogResult.OK){
       using (System.IO.FileInfo fileToSave = new System.IO.FileInfo(fileDialog.FilePath)){
          MemoryStream ms = System.IO.FileStream(fileToSave.FullNae, IO.FileMode.Open);
           //Here you can copy the ms over to a byte array to save into your blob in your database.
       }

   }
}
msarchet
thanks for the answer but what do "MemoryStream ms = System.IO.FileStream(fileToSave.FullNae, IO.FileMode.Open);" do?
kraven
IT creates a new MemoryStream object (basically a byte array stored in memory) by reading the contents of the selected file into the memorystream
msarchet
A: 

Example:

string filename = "foo.txt"; // TODO: fetch from file dialog
byte[] buffer = File.ReadAllBytes(filename);

using (var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=foo.mdb"))
using (var cmd = conn.CreateCommand())
{
    cmd.CommandText = "INSERT INTO MyTable VALUES (@Name, @Data)";
    cmd.Parameters.AddWithValue("@Name", filename);
    cmd.Parameters.AddWithValue("@Data", buffer);
    cmd.ExecuteNonQuery();
}
Darin Dimitrov
@Darin Dimitrov I like that both our answers together is the full answer.
msarchet
@Darin Dimitrov thanks but if I want to store an image string change to something?
kraven
I get and error I am using the new format of Acces accdb, what can I do?
kraven
but now how can I be sure that is stored in the database Acces? my code is in the begining
kraven
A: 

I would use File.ReeadAllBytes(file) and save the byte[] there in the DB.

Derar