.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Read / Write Image into SQL Server in ASP.NET Part - 2

Posted By:Pankaj Mishra       Posted Date: September 01, 2008    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Hi,

In this articles we will read the image from datatbase (SqlServer) and display in our webpage. In Part 1 we have seen how to save image in SQL Srever http://www.dotnetspark.com/kb/35-read--write-image-into-sql-server-asp-net.aspx

In Page load(or which ever event you want) Event you can write the below code to fetch Saved Image from SQL Server.

SqlConnection myConn = new SqlConnection (@"server=Myserver;database=MyDB;uid=sa;pwd=sa");
    MemoryStream MyStream = new MemoryStream ();
   
    try
    {
        connection.Open ();
        SqlCommand command = new SqlCommand ("select Pic from ImageTable", myConn);
        byte[] bytePic = (byte[]) command.ExecuteScalar ();  
        MyStream.Write (bytePic, 0, bytePic.Length);
        Bitmap bitmap = new Bitmap (MyStream);
        Response.ContentType = "image/jpg";
        bitmap.Save (Response.OutputStream, ImageFormat.jpg);
    }
    catch(Exception ex)
    {
       throw ex;
    }
    finally
    {
        connection.Close ();
        stream.Close ();
    }
 

Cheers
Pankaj

 Subscribe to Articles

     

Further Readings:

Responses
Author: Anantha         Company URL: http://www.dotnetspark.com
Posted Date: September 06, 2008

hi..
thank you for this information.. but how to retrive multiple pic at a time..? now it is retriving only one pic.. thanks in advance..
bye
Author: Pankaj Mishra         Company URL: http://www.dotnetspark.com
Posted Date: September 08, 2008

Hi,
Use DataSet Or DataReader to execute query and retrive multiple rows and you can cast that to (byte[]).

Hope this will help you

Cheers
Pankaj

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend