Use Asp.net to upload and store images to sqlserver, and then read and display images from sqlserver.

Source: Internet
Author: User

1. Upload and store the data to sqlserver
Database Structure
Create Table Test
{
Id identity (1, 1 ),
Fimage Image
}
Related stored procedures
Create proc updateimage
(
@ Updateimage Image
)
As
Insert into test (fimage) values (@ updateimage)
Go

Add the following to the upphoto. aspx file:
<Input id = "upphoto" name = "upphoto" runat = "server" type = "file">
<Asp: button id = "btnadd" name = "btnadd" runat = "server" text = "Upload"> </ASP: button>

ThenCodeFile upphoto. aspx. CS add btnadd button click event processing code:
Private void btnadd_click (Object sender, system. eventargs E)
{
// Obtain the image and convert the image to byte []
Httppostedfile upphoto = upphoto. postedfile;
Int upphotolength = upphoto. contentlength;
Byte [] photoarray = new byte [upphotolength];
Stream photostream = upphoto. inputstream;
Photostream. Read (photoarray, 0, upphotolength );

// Connect to the database
Sqlconnection conn = new sqlconnection ();
Conn. connectionstring = "Data Source = localhost; database = test; user id = sa; Pwd = sa ";

Sqlcommand cmd = new sqlcommand ("updateimage", Conn );
Cmd. commandtype = commandtype. storedprocedure;

Cmd. Parameters. Add ("@ updateimage", sqldbtype. Image );
Cmd. Parameters ["@ updateimage"]. value = photoarray;

// If you do not want to use the stored procedure to add an image, replace the above four statements with the following code:
// String strsql = "insert into test (fimage) values (@ fimage )";
// Sqlcommand cmd = new sqlcommand (strsql, Conn );
// Cmd. Parameters. Add ("@ fimage", sqldbtype. Image );
// Cmd. Parameters ["@ fimage"]. value = photoarray;

Conn. open ();
Cmd. executenonquery ();
Conn. Close ();
}

2. Read and display data from sqlserver
Add the following code to the image display area:
<Asp: Image id = "imgphoto" runat = "server" imageurl = "showphoto. aspx"> </ASP: Image>

Showphoto. aspx subject code:
Private void page_load (Object sender, system. eventargs E)
{
If (! Page. ispostback)
{
Sqlconnection conn = new sqlconnection ()
Conn. connectionstring = "Data Source = localhost; database = test; user id = sa; Pwd = sa ";

String strsql = "select * from test where id = 2"; // assume that the image with ID 2 is obtained.
Sqlcommand cmd = new sqlcommand ()
Reader. Read ();
Response. contenttype = "application/octet-stream ";
Response. binarywrite (byte []) Reader ["fimage"]);
Response. End ();
Reader. Close ();
}
}

3. Store the image in winform to sqlserver and read it from sqlserver and display it in picturebox.

1. store the data in sqlserver
The database structure and stored procedures used are the same as above.
1.1 add an openfiledialog control in the form and name it ofdselectpic
1.2. Add an open file button on the form and add the following Event code:
Stream MS;
Byte [] picbyte;
// Ofdselectpic. showdialog ();
If (ofdselectpic. showdialog () = dialogresult. OK)
{
If (MS = ofdselectpic. openfile ())! = NULL)
{
// MessageBox. Show ("OK ");
Picbyte = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (picbyte, 0, convert. toint32 (Ms. Length ));
// MessageBox. Show ("read finished! ");

// Connect to the database
Sqlconnection conn = new sqlconnection ();
Conn. connectionstring = "Data Source = localhost; database = test; user id = sa; Pwd = sa ";

Sqlcommand cmd = new sqlcommand ("updateimage", Conn );
Cmd. commandtype = commandtype. storedprocedure;

Cmd. Parameters. Add ("@ updateimage", sqldbtype. Image );
Cmd. Parameters ["@ updateimage"]. value = picbyte;

Conn. open ();
Cmd. executenonquery ();
Conn. Close ();

Ms. Close ();
}
}

2. Read and display in picturebox
2.1 Add a picturebox named ptbshow
2.2 Add a button to add the following response event:
Sqlconnection conn = new sqlconnection ();
Conn. connectionstring = "Data Source = localhost; database = test; user id = sa; Pwd = sa ";

String strsql = "select fimage from test where id = 1 ";

Sqlcommand cmd = new sqlcommand (strsql, Conn );

Conn. open ();
Sqldatareader reader = cmd. executereader ();
Reader. Read ();

Memorystream MS = new memorystream (byte []) Reader ["fimage"]);

Image image = image. fromstream (MS, true );

Reader. Close ();
Conn. Close ();

Ptbshow. Image = image;

Add the following to the upphoto. aspx file:
<Input id = "upphoto" name = "upphoto" runat = "server" type = "file">
<Asp: button id = "btnadd" name = "btnadd" runat = "server" text = "Upload"> </ASP: button>

Then, add the btnadd button to the post code file upphoto. aspx. CS and click the event processing code:
Private void btnadd_click (Object sender, system. eventargs E)
{
// Obtain the image and convert the image to byte []
Httppostedfile upphoto = upphoto. postedfile;
Int upphotolength = upphoto. contentlength;
Byte [] photoarray = new byte [upphotolength];
Stream photostream = upphoto. inputstream;
Photostream. Read (photoarray, 0, upphotolength );

// Connect to the database
Sqlconnection conn = new sqlconnection ();
Conn. connectionstring = "Data Source = localhost; database = test; user id = sa; Pwd = sa ";

Sqlcommand cmd = new sqlcommand ("updateimage", Conn );
Cmd. commandtype = commandtype. storedprocedure;

Cmd. Parameters. Add ("@ updateimage", sqldbtype. Image );
Cmd. Parameters ["@ updateimage"]. value = photoarray;

// If you do not want to use the stored procedure to add an image, replace the above four statements with the following code:
// String strsql = "insert into test (fimage) values (@ fimage )";
// Sqlcommand cmd = new sqlcommand (strsql, Conn );
// Cmd. Parameters. Add ("@ fimage", sqldbtype. Image );
// Cmd. Parameters ["@ fimage"]. value = photoarray;

Conn. open ();
Cmd. executenonquery ();
Conn. Close ();
}

2. Read and display data from sqlserver
Add the following code to the image display area:
<Asp: Image id = "imgphoto" runat = "server" imageurl = "showphoto. aspx"> </ASP: Image>

Showphoto. aspx subject code:
Private void page_load (Object sender, system. eventargs E)
{
If (! Page. ispostback)
{
Sqlconnection conn = new sqlconnection ()
Conn. connectionstring = "Data Source = localhost; database = test; user id = sa; Pwd = sa ";

String strsql = "select * from test where id = 2"; // assume that the image with ID 2 is obtained.
Sqlcommand cmd = new sqlcommand ()
Reader. Read ();
Response. contenttype = "application/octet-stream ";
Response. binarywrite (byte []) Reader ["fimage"]);
Response. End ();
Reader. Close ();
}
}

3. Store the image in winform to sqlserver and read it from sqlserver and display it in picturebox.

1. store the data in sqlserver
The database structure and stored procedures used are the same as above.
1.1 add an openfiledialog control in the form and name it ofdselectpic
1.2. Add an open file button on the form and add the following Event code:
Stream MS;
Byte [] picbyte;
// Ofdselectpic. showdialog ();
If (ofdselectpic. showdialog () = dialogresult. OK)
{
If (MS = ofdselectpic. openfile ())! = NULL)
{
// MessageBox. Show ("OK ");
Picbyte = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (picbyte, 0, convert. toint32 (Ms. Length ));
// MessageBox. Show ("read finished! ");

// Connect to the database
Sqlconnection conn = new sqlconnection ();
Conn. connectionstring = "Data Source = localhost; database = test; user id = sa; Pwd = sa ";

Sqlcommand cmd = new sqlcommand ("updateimage", Conn );
Cmd. commandtype = commandtype. storedprocedure;

Cmd. Parameters. Add ("@ updateimage", sqldbtype. Image );
Cmd. Parameters ["@ updateimage"]. value = picbyte;

Conn. open ();
Cmd. executenonquery ();
Conn. Close ();

Ms. Close ();
}
}

2. Read and display in picturebox
2.1 Add a picturebox named ptbshow
2.2 Add a button to add the following response event:
Sqlconnection conn = new sqlconnection ();
Conn. connectionstring = "Data Source = localhost; database = test; user id = sa; Pwd = sa ";

String strsql = "select fimage from test where id = 1 ";

Sqlcommand cmd = new sqlcommand (strsql, Conn );

Conn. open ();
Sqldatareader reader = cmd. executereader ();
Reader. Read ();

Memorystream MS = new memorystream (byte []) Reader ["fimage"]);

Image image = image. fromstream (MS, true );

Reader. Close ();
Conn. Close ();

Ptbshow. Image = image;

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.