Save the asp.net image to the database, read the image from the database, and display it

Source: Internet
Author: User

This article summarizes two questions about saving images in asp.net to the database, reading images from the database, and displaying program code.

Example 1

The Code is as follows: Copy code

Private void button2_Click_1 (object sender, System. EventArgs e)

{

String pathName;

If (this. openFileDialog1.ShowDialog () = System. Windows. Forms. DialogResult. OK)

{

PathName = this. openFileDialog1.FileName;

System. Drawing. Image img = System. Drawing. Image. FromFile (pathName );

This. pictureBox1.Image = img;

// Read the image into a byte array

System. IO. FileStream fs = new System. IO. FileStream (pathName, System. IO. FileMode. Open, System. IO. FileAccess. Read );

Byte [] buffByte = new byte [fs. Length];

Fs. Read (buffByte, 0, (int) fs. Length );

Fs. Close ();

Fs = null;

// CREATE Command

String comm = @ "Insert into table1 (img, name) values (@ img, @ name )";

This. sqlCommand1 = new System. Data. SqlClient. SqlCommand ();

This. sqlCommand1.CommandType = System. Data. CommandType. Text;

This. sqlCommand1.CommandText = comm;

This. sqlCommand1.Connection = this. sqlConnection1;

// Create a Parameter

This. sqlCommand1.Parameters. Add ("@ img", System. Data. SqlDbType. Image );

This. sqlCommand1.Parameters [0]. Value = buffByte;

This. sqlCommand1.Parameters. Add ("@ name", System. Data. SqlDbType. VarChar );

This. sqlCommand1.Parameters [1]. Value = pathName. Substring (pathName. LastIndexOf ("\") + 1 );

Try

{

This. sqlConnection1.Open ();

This. sqlCommand1.ExecuteNonQuery ();

This. sqlConnection1.Close ();

}

Catch (System. Exception ee)

{

MessageBox. Show (ee. Message );

}

BuffByte = null;

This. FillListBox ();

}


Read:

Reading images from databases to picturebox

The Code is as follows: Copy code


SqlConnection conn = new SqlConnection (@ "data source = chenyuming2004VSdotNET; uid = sa; pwd = cym; database = lhf ");

Conn. Open ();

SqlCommand cmd = new SqlCommand ("select photo from fuser where password = '1b '", conn );

SqlDataReader reader = cmd. ExecuteReader ();

Reader. Read ();

MemoryStream buf = new MemoryStream (byte []) reader [0]);

Image image = Image. FromStream (buf, true );

PictureBox1.Image = image;

Example 2

How to save images to a database:

The Code is as follows: Copy code

Public void imgToDB (string SQL)
{// The imge variable name required to be saved in the parameter SQL is @ images
// Call method such as imgToDB ("update UserPhoto set Photo = @ images where UserNo = '" + temp + "'");
FileStream fs = File. OpenRead (t_photo.Text );
Byte [] imageb = new byte [fs. Length];
Fs. Read (imageb, 0, imageb. Length );
Fs. Close ();
SqlCommand com3 = new SqlCommand (SQL, con );
Com3.Parameters. Add ("@ images", SqlDbType. Image). Value = imageb;
If (com3.Connection. State = ConnectionState. Closed)
Com3.Connection. Open ();
Try
{
Com3.ExecuteNonQuery ();
}
Catch
{}
Finally
{Com3.Connection. Close ();}
}

Read the image in the database and display it in picturebox:

Method 1:

The Code is as follows: Copy code
Private void ShowImage (string SQL)
{
// Call method such as ShowImage ("select Photo from UserPhoto where UserNo = '" + userno + "'");
SqlCommand cmd = new SqlCommand (SQL, conn );
Conn. Open ();
Byte [] B = (byte []) cmd. ExecuteScalar ();
If (B. Length> 0)
{
MemoryStream stream = new MemoryStream (B, true );
Stream. Write (B, 0, B. Length );
PictureBox1.Image = new Bitmap (stream );
Stream. Close ();
}
Conn. Close ();
}

Method 2: select a row in dg:

The Code is as follows: Copy code
Private void dg_MouseUp (object sender, MouseEventArgs e)
{
// Select the entire row
If (e. Button = System. Windows. Forms. MouseButtons. Left)
{// User ID, name, gender, ID card number, nationality, school, department, campus, department, phone number, photo
// Display the photo
Object imgobj = dg [10, dg. CurrentRow. Index]. Value;
If (imgobj! = Null &&! Convert. IsDBNull (imgobj ))
{
Byte [] imgb = (byte []) imgobj;
MemoryStream memStream = new MemoryStream (imgb );
Try
{
Bitmap myimge = new Bitmap (memStream );
This. pictureBox1.Image = myimge;
}
Catch
{
DB. msgbox ("An error occurred while reading photos from the database! ");
}
}
Else
PictureBox1.Image = null;
}
}

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.