Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

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

Author: Mike Green
Download Source Code : 1397_SaveImagesInAccess.zip

I will explain in this article that how we can save image data in MS Access database using ASP.NET.

 

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.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to transfer selected rows from one GridView to another GridView in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net