top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to save images in MS Access database in ASP.NET?

0 votes
727 views
How to save images in MS Access database in ASP.NET?
posted Feb 15, 2016 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

We can save images in MS Access database in binary format as in SQL Server database. We have to set data type of binary data field as “OLE Object ”. In ASP.NET code, we need to read image data as bytes using Stream class and then we can store these bytes into MS Access database.

I have created a MS Access database “ImagesDB” and created a table in this database “Images”. I have added following fields in the table.

ImageID (Data Type: AutoNumber)
ImageName (Data Type: Text)
ImageSize (Data Type: Number)
ImageData (DataType: OLE Object)

1.Open MS Visual Studio 2010
2.File > New > Website > Visual C# or Visual Basic > ASP.NET Empty Web Site
3.Select Web Location as File System and Click OK
4.From Menu, Website > Add New Item > Select Web Form and Click Add.
5.Write below connection string in web.config file

<connectionStrings>
    <add name="ImagesDB" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ImagesDB.accdb"/>
</connectionStrings>

6.Write code below in Default.aspx page

<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>

7.Include following namespaces in your code file

C#

using System.IO;
using System.Configuration;
using System.Data.OleDb;
using System.Data;

VB.NET

Imports System.IO
Imports System.Configuration
Imports System.Data.OleDb
Imports System.Data

8.Write below code in Button click event to save image in MS Access Database

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["ImagesDB"].ConnectionString;
       OleDbConnection connection = new OleDbConnection(connString);

       string insertQuery = "INSERT INTO Images(ImageName, ImageSize, 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();
        }
    }
}

VB.NET

Protected Sub btnSaveImage_Click(sender As Object, e As System.EventArgs) Handles btnSaveImage.Click
    If FileUpload1.HasFile Then
        Dim name As String = FileUpload1.PostedFile.FileName
        Dim length As Integer = FileUpload1.PostedFile.ContentLength

        Dim imageBytes As Byte() = New Byte(length - 1) {}
        Dim imageStream As Stream = FileUpload1.PostedFile.InputStream
        imageStream.Read(imageBytes, 0, length)

        Dim connString As String = ConfigurationManager.ConnectionStrings("ImagesDB").ConnectionString
        Dim connection As New OleDbConnection(connString)

        Dim insertQuery As String = "INSERT INTO Images(ImageName, ImageSize, ImageData) VALUES(@ImageName, @ImageSize, @ImageData)"
        Dim command As 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 ex As Exception
            lblMessage.Text = "Unable to save image data"
        Finally
            connection.Close()
        End Try
    End If
End Sub

First we have retrieved image name and size then we have declared a byte array. We have created an object of Stream class for the file to read content. Read() method will read sequence of bytes for uploaded file. We have set our connection to MS Access database using connection string in web.config file. Then we have used an insert query to insert image data to MS Access database. We have created an object for OledbCommand class, provided command text and type and added parameters to object. We have tried to open our connection and to call ExcecuteNonQuery() method of OleDbCommand object in Try block to save image to MS Access database.

9.Now you can browse the website and save image in MS Access database.

answer Feb 15, 2016 by Shivaranjini
Similar Questions
+4 votes

I have my Code . Kindly make it correct .

  protected void Button1_Click(object sender, EventArgs e)
  {
        System.Threading.Thread.Sleep(500);
        Image1.ImageUrl = "Below15.png";
        System.Threading.Thread.Sleep(500);
        Image1.ImageUrl = "Below26.png";
        System.Threading.Thread.Sleep(500);
        Image1.ImageUrl = "Below31.png";
        System.Threading.Thread.Sleep(500);
        Image1.ImageUrl = "Below40.png";
        System.Threading.Thread.Sleep(500);
        Image1.ImageUrl = "Above70.png";
 }
+3 votes

I want to save specific html portion into pdf using asp.net into MS SQL SERVER 2008. I know it will done will itextsharp.dll library file from .NET conversion but tell me through example.

...