Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of storing pictures in mysql

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the method of storing pictures in mysql, which is very detailed and has certain reference value. Friends who are interested must finish reading it.

The method of storing the picture in mysql: 1, get the picture to be saved; 2, upload the picture to the folder under the specified path; 3, save the path to the variable, and save the value of the variable to the corresponding field in the database.

There are generally two specific methods:

1. Store the path where the picture is saved to the database

2. Write the picture directly to the database field in the form of binary data stream.

First, save the upload path of the image to the database:

String uppath= ""; / / used to save the upload path of the picture / / get the file name of the uploaded picture string fileFullname = this.FileUpload1.FileName; / / get the upload time of the picture. Using the time as the name of the picture can prevent the picture from repeating the name string dataName = DateTime.Now.ToString ("yyyyMMddhhmmss"); / / get the file name of the picture (without extension) string fileName = fileFullname.Substring (fileFullname.LastIndexOf ("\") + 1) / / get the picture extension string type = fileFullname.Substring (fileFullname.LastIndexOf (".") + 1) / / determine whether it is the required format if (type = = "bmp" | | type = = "jpg" | | type = = "jpeg" | | type = = "gif" | | type = = "JPG" | | type = "JPEG" | | type = = "BMP" | | type = = "GIF") {/ / upload the image to the folder this.FileUpload1.SaveAs (Server.MapPath ("~ / upload") + "\" + dataName + "." + type) in the specified path. / / Save the path to a variable, and save the value of the variable to the corresponding field in the database, uppath = "~ / upload/" + dataName + "." + type;}

Second, save the picture in binary data stream directly to the database:

Reference the following namespace: when using System.Drawing; using System.IO; using System.Data.SqlClient; designs the database, the corresponding field type in the table is saved as iamge: / / picture path string strPath = this.FileUpload1.PostedFile.FileName.ToString (); / / read picture FileStream fs = new System.IO.FileStream (strPath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader (fs) Byte [] photo = br.ReadBytes ((int) fs.Length); br.Close (); fs.Close (); / / store SqlConnection myConn = new SqlConnection ("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123"); string strComm = "INSERT INTO stuInfo (stuid,stuimage) VALUES (107 camera photobinary)"; / / modify SqlCommand myComm = new SqlCommand (strComm, myConn) as needed. MyComm.Parameters.Add ("@ photoBinary", SqlDbType.Binary, photo.Length); myComm.Parameters ["@ photoBinary"]. Value = photo; myConn.Open (); if (myComm.ExecuteNonQuery () > 0) {this.Label1.Text = "ok";} myConn.Close (); read:. The connection database string is omitted mycon.Open (); SqlCommand command = new SqlCommand ("select stuimage from stuInfo where stuid=107", mycon); / / the query statement modifies byte [] image = (byte []) command.ExecuteScalar () as needed; / / specifies the save path and name of the picture read from the database string strPath = "~ / Upload/zhangsan.JPG"; string strPhotoPath = Server.MapPath (strPath) / / Save the image file BinaryWriter bw = new BinaryWriter (File.Open (strPhotoPath,FileMode.OpenOrCreate)); bw.Write (image); bw.Close (); / / display the picture this.Image1.ImageUrl = strPath according to the path and name above

Using these two methods, we can choose flexibly according to the actual needs.

The above is all the contents of the method of storing pictures in mysql. Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report