Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 17-01-2009
Member
 
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 ?
Reply With Quote
  #2  
Old 17-01-2009
Member
 
Join Date: May 2008
Posts: 2,008
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();
  }
}
Reply With Quote
  #3  
Old 17-01-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 11:57 PM.