Inserting the picture into the database

Discussion in 'ASP .Net Web Controls' started by Comfort, Oct 5, 2004.

  1. Comfort

    Comfort Guest

    I have tried the following way and fail!! I was using VB.NET. Is that the
    reason? How can I get the file control and how can I convert the file
    control into the server object. Below is the code I used:



    Introduction

    This sample helps you to store and retrieve Images in any format into MS SQL
    Server Database tables through ASP.NET Application. Create a table with 3
    fields with a field as 'Image' datatype. Add new asp.net project and add
    new web form. Add a File control and name it as 'File1' and make it as
    Server Control. Add a Submit button.

    Press F7 to view the webpage Code-behind and add the following code. The
    code given here is in C#.

    Methods to Establish Database Connection

    The method 'OpenConnection' helps to create database connection. 'Con' is
    connection object.


    public SqlConnection Con;
    public void OpenConnection()
    {
    string
    cnstr="Server=YourServerName;Database=YourDatabaseName;Uid=YourUserId;Pwd=Yo
    urPassword;";
    Con=new SqlConnection(cnstr);
    Con.Open();
    }



    Methods to Store and Retrieve Images

    The method 'GetImage' gets the images that is stored in the database. The
    method 'InsertImage' stores Images into database.


    public byte[] GetImage(string ImageId)
    {
    string sql="Select Img_Bin from Images where Img_Code=@ImageId";
    OpenConnection();
    SqlCommand cmd=new SqlCommand(sql,Con);
    cmd.Parameters.Add("@ImageId",SqlDbType.Int).Value=ImageId;
    cmd.Prepare();
    SqlDataReader dr=cmd.ExecuteReader();
    dr.Read();
    return (byte[])dr["Img_Bin"];
    }


    public void InsertImage(string ImgCode,byte[] Images,BigInt ContentLength)
    {
    string sql="Insert into Pot_Images (Img_Code,Img_Bin,Img_Len) values
    (@ImgCode,@Images,@ImgLen)";
    OpenConnection();
    SqlCommand cmd=new SqlCommand(sql,Con);
    cmd.Parameters.Add("@ImgCode",SqlDbType.Char,10).Value=ImgCode;
    cmd.Parameters.Add("@Images",SqlDbType.Image,ContentLength).Value=Images;
    cmd.Parameters.Add("@ImgLen",SqlDbType.BigInt).Value=ContentLength;
    cmd.Prepare();
    cmd.ExecuteNonQuery();
    }



    Inserting and Viewing Images from Webform
    Double click the Submit button and copy the code below.


    byte[] ImageStr=new byte[File1.PostedFile.ContentLength];
    HttpPostedFile Image=File1.PostedFile;
    Image.InputStream.Read(ImageStr,0,(int)File1.PostedFile.ContentLength);
    InsertImage('1',ImageStr,File1.PostedFile.ContentLength);
    Response.BinaryWrite(GetImage('1'));



    Explanation of the Concept
    Here we take a image in JPG,BMP or any format, and store it into database.
    First we convert the image as binary stream with the help of
    InputStream.Read method. Then we pass these information to insert into the
    database. Then we can retrieve the binary data from database to view the
    Image using Response.BinaryWrite method.
     
    Comfort, Oct 5, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    400
  2. wjer
    Replies:
    1
    Views:
    493
    Alvin Bruney [MVP]
    Mar 2, 2004
  3. Curt_C [MVP]
    Replies:
    3
    Views:
    14,309
    jharr100
    Aug 14, 2009
  4. Tampa .NET Koder
    Replies:
    0
    Views:
    515
    Tampa .NET Koder
    Jul 27, 2004
  5. Replies:
    8
    Views:
    1,270
    Mick White
    Apr 19, 2005
Loading...

Share This Page