Results 1 to 3 of 3

Thread: Storing images in SQL database using Asp.net

  1. #1
    Join Date
    Dec 2008
    Posts
    7

    question Storing images in SQL database using Asp.net

    How can I Store images in SQL database using Asp.net ?

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Storing images in SQL database using Asp.net

    The following code shows how to use ADO.NET to store image field in SQL Server.

    Code:
    public void StorePicture( string filename )
    {
      // Read the file into a byte array
      using(FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
      {
        byte[] imageData = new Byte[fs.Length];
        fs.Read( imageData, 0, (int)fs.Length );
      }
    
      using( SqlConnection conn = new SqlConnection(connectionString) )
      {
        SqlCommand cmd = new SqlCommand("StorePicture", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@filename", filename );
        cmd.Parameters["@filename"].Direction = ParameterDirection.Input;
        cmd.Parameters.Add("@blobdata", SqlDbType.Image);
        cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;
        // Store the byte array within the image field
        cmd.Parameters["@blobdata"].Value = imageData;
        conn.Open();
        cmd.ExecuteNonQuery();
      }
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Storing images in SQL database using Asp.net

    You can use this stored procedure to add image to sql database manually.

    Code:
    CREATE       PROCEDURE [sp_AddImage] 
            (    
                    @img_pk        INT         OUTPUT    ,
                    @img_data        IMAGE                
            )
    AS 
        INSERT INTO tbl_Image ( img_data)
        VALUES ( @img_data )
    
        IF @@ROWCOUNT=1
        SET @img_pk=@@IDENTITY
    GO

Similar Threads

  1. How to use ADO.NET Entity Framework to obtain images from database
    By Galbraith in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 02:10 AM
  2. Storing database data into Map
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 01:17 AM
  3. Inserting and retrieving images from Database
    By Ainsley in forum Software Development
    Replies: 3
    Last Post: 25-01-2010, 01:49 PM
  4. Storing photos on dvd
    By mohandas in forum Windows Software
    Replies: 3
    Last Post: 06-08-2009, 12:53 PM
  5. Storing MAC addresses in AD
    By boris52 in forum Active Directory
    Replies: 4
    Last Post: 27-07-2009, 10:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,886,341.94131 seconds with 17 queries