Use asp.net to upload pictures and save them to SQL Server, and then read and display them from SQL Server

Source: Internet
Author: User
asp.net|server|sqlserver| Upload | show

1, use asp.net to upload pictures and save them to SQL Server, then read and display them from SQL Server
One, upload and deposit SQL Server
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 in 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 Click event handling code for the Btnadd button in the post code file UpPhoto.aspx.cs:
private void btnAdd_Click (object sender, System.EventArgs e)
{
Get 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);

Connecting to a 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 want to add a picture without using a stored procedure, change the above four code to read:
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 ();
}

Second, read and display from SQL Server
Add the following code where you want the picture to appear:
<asp:image id= "Imgphoto" runat= "Server" imageurl= "showphoto.aspx" ></asp:image>

Showphoto.aspx Body 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";//here suppose you get a picture with ID 2
SqlCommand cmd=new SqlCommand ()
Reader. Read ();
Response.contenttype= "Application/octet-stream";
Response.BinaryWrite ((byte[]) reader["Fimage");
Response.End ();
Reader. Close ();
}
}


3, save the picture in SQL Server in WinForm and read it from SQL Server and display it in PictureBox

1, stored in SQL Server
Database structure and stored procedures used, same as above
1.1, add a OpenFileDialog control to the form named Ofdselectpic
1.2, add an open File button on the form and add the following click 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!");

Connecting to a 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 in 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 Click event handling code for the Btnadd button in the post code file UpPhoto.aspx.cs:
private void btnAdd_Click (object sender, System.EventArgs e)
{
Get 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);

Connecting to a 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 want to add a picture without using a stored procedure, change the above four code to read:
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 ();
}

Second, read and display from SQL Server
Add the following code where you want the picture to appear:
<asp:image id= "Imgphoto" runat= "Server" imageurl= "showphoto.aspx" ></asp:image>

Showphoto.aspx Body 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";//here suppose you get a picture with ID 2
SqlCommand cmd=new SqlCommand ()
Reader. Read ();
Response.contenttype= "Application/octet-stream";
Response.BinaryWrite ((byte[]) reader["Fimage");
Response.End ();
Reader. Close ();
}
}


3, save the picture in SQL Server in WinForm and read it from SQL Server and display it in PictureBox

1, stored in SQL Server
Database structure and stored procedures used, same as above
1.1, add a OpenFileDialog control to the form named Ofdselectpic
1.2, add an open File button on the form and add the following click 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!");

Connecting to a 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;



Related Article

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.