SQL 2008 Store pictures and SQL 2008 Read pictures

Source: Internet
Author: User
Tags sql 2008

Storing text data with SQL Server is easy to implement, and if you store pictures with SQL Server? Do you have any idea of realizing it? Now I'm using a demo to provide you with a way to store images in SQL Server.

Scene: In the Superphoto folder, there are three NBA superstar images that need to be stored in the database, they are Paul, Ross and DeLong, the file contents are as follows:

With the demand, the following is achieved. The implementation process is divided into 3 steps.

Step 1: We need to create the T_superstar table in the database test and insert the information of the three-bit star into the table.

CREATE TABLE T_superstar (Superid varchar (TEN) primary key,supername varchar () not null,superphoto varchar (TEN) NOT null) I Nsert into T_superstar values (' 1 ', ' Paul ', ' 001.png ') insert into T_superstar values (' 2 ', ' Ross ', ' 002.png ') insert INTO T_ SuperStar values (' 3 ', ' de lung ', ' 003.png ')
The table structure looks like this:


The query results reveal that the Superphoto field stores the file name of the picture file. This idea is like in-memory stacks and heaps, and we all know that stacks are used to store value type data because the data size of the value type is fixed, and the heap is used to store reference data such as objects, arrays, and so on. When you add an object, you add a reference to the object in the stack, in addition to adding the object to the heap.

Similarly, because the picture file is larger, put in the database is very space, so we put the picture in a heap-like folder, put the file name in a stack-like data table, the file name to lock the target files.

Step 2: Add the GridView on the ASP. NET page and add columns for the GridView, especially for the operation of the picture column. We need to configure the dynamic load path of the picture for the picture column, as shown in the configuration diagram:


The properties of the Picture column Dataimageurlfield the field Superphoto in the bound data table, The Dataimageurlformat property dynamically loads the picture file under the Superphoto folder according to the file name read by Dataimageurlfield, enabling dynamic reading of the stored image.

Step 3: Get the data for the T_superstar table in C # and bind to the GridView.

        protected void Page_Load (object sender, EventArgs e)        {            if (!this. IsPostBack)            {                SqlConnection con = DB. Createcon ();                Con. Open ();                SqlCommand cmd = new SqlCommand ("SELECT * from T_superstar", con);                SqlDataReader SDR = cmd. ExecuteReader ();                This. Gridview1.datasource = SDR;                This. Gridview1.databind ();            }        }
to this, the picture is stored and the code portion of the read is finished and we look at it.

It people know that learning computer science for a long time will form a computer thinking, with this thinking to solve the problems encountered in peacetime, I would like to say that I recruit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.