2

All I want to do is add pictures to my database via my C# project and yet be able to open it (the picture) when working in Access itself… I found code to enter it into the database and according to Access the picture has been added to the table, but I can’t open it from within Access itself, saying that I need to restart my OLE Server.
The example’s database has 3 columns: ‘Imagename”, “Imagesize” and “Imagedata”.
“Imagedata” is an OLEDB-type while the rest is a “Text”-type.

(Ps. I’m using C# 2012” and Access 2010)

The example looks like this:

ASP.NET side:

      <form runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="btnSaveImage" runat="server" Text="Save in MS Access" OnClick="btnSaveImage_Click" />
<br />
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>

</form>

The C# side

    protected void btnSaveImage_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        string name = FileUpload1.PostedFile.FileName;
        int length = FileUpload1.PostedFile.ContentLength;

        byte[] imageBytes = new byte[length];
        Stream imageStream = FileUpload1.PostedFile.InputStream;
        imageStream.Read(imageBytes, 0, length);

        string connString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
        OleDbConnection connection = new OleDbConnection(connString);

        string insertQuery = "INSERT INTO Images(ImageData) VALUES(@ImageName, @ImageSize, @ImageData)";
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = insertQuery;
        command.CommandType = CommandType.Text;
        command.Parameters.AddWithValue("@ImageName", name);
        command.Parameters.AddWithValue("@ImageSize", length);
        command.Parameters.AddWithValue("@ImageData", imageBytes);

        try
        {
            connection.Open();
            command.ExecuteNonQuery();
            lblMessage.Text = "Image data saved successfully";
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Unable to save image data";
        }
        finally
        {
            connection.Close();
        }
    }
}

and web.config looks like this:

    <connectionStrings>
    <add name="ConnectionString2" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\Anja\Documents\sitprentein.accdb'"/>
</connectionStrings>

Even though the code works and embeds the images into my database, I can’t open it from within Access. Images that were embedded directly from within Access display the word “Package” in the cell, whereas the images that were inserted via C# display “long binary data” When I double-click on the “Package” the file opens and displays my images, but when clicking on , “long binary data” I’m told to restart the OLE Server from outside of Access and then to try again.

I’ve searched the internet but the answers I found doesn’t work. I’ve even tried re-installing Office, but it didn’t work either. Is there a way that I can insert my images as OLEDB objects in my database (via C#) in such a way that I will still be able to open them from within Access? And if there is, can someone please be so kind as to help me?

Eager and hoping for a positive reply. Diedomkop

diedomkop
  • 21
  • 2
  • You are aware storing lots of objects in db can quickly use up Access 2GB size limit? – June7 Sep 01 '21 at 16:56
  • Review https://medium.com/@haggenso/export-ole-fields-in-microsoft-access-c67d535c958d, says to use OleDbParameter. – June7 Sep 01 '21 at 17:08
  • Better off to use a memo type field. and to display picutre in access you then write out the memo field to a local temp file, and then have access display the actual picture with the image control and a path name. This also will reduce bloat. The next better way is to use the Access attachment system - but I don't know if ado.net supports it. But I would avoid the older ole image system - use the newer access attachment system - it does not bloat. (I try and check if ado.net supports the attachment system in access - not sure it does). I would store image outside of db if possible. – Albert D. Kallal Sep 01 '21 at 17:19
  • See [this](https://stackoverflow.com/a/46793834/7296893) to display an image in Access. Saving it as anything but a long binary OLE field is a menace for migration (and of course, don't write binary data to text fields). – Erik A Sep 01 '21 at 21:20
  • I would put the image file into a folder, not into the Access db. ... and then only insert a text string into an Access db table that is the path to the image file. ... Access forms/reports are able to call in the image from the folder. – Cahaba Data Sep 01 '21 at 23:06

0 Answers0