C # Save the image to the database and read the image from the database and display it,
How to save images to a database:
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 inPictureboxMedium:
Method 1:
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:
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;
}
UseC #Accessing image databases
This article summarizes how to save images to SQL Server in. Net WinForm and. Net WebForm (asp.net) and read the display method.
1. Use asp.net to upload images and store them in SQL Server. Then, read and display the images from SQL Server:
1) Upload and store the data to 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 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 from SQL Server
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 (strSql, conn );
Conn. Open ();
SqlDataReader reader = cmd. ExecuteReader ();
Reader. Read ();
Response. ContentType = "application/octet-stream ";
Response. BinaryWrite (Byte []) reader ["FImage"]);
Response. End ();
Reader. Close ();
}
}
2. Store the image to SQL Server in WinForm, read it from SQL Server, and display it in picturebox.
1), stored in SQL Server
The database structure and stored procedures used are the same as above.
First, add an OpenFileDialog control in the form and name it ofdSelectPic;
Then, 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
First, add a picturebox named ptbShow
Then, 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;
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.