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