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

Can mysql database store pictures?

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

Share

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

This article mainly introduces whether the mysql database can store pictures or not, which has certain reference value and can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.

MySQL is a relational database management system developed by the Swedish company MySQL AB and currently belongs to the products of Oracle. Its function is very powerful, some of which may not be very clear, for example, it can also store pictures, do you know that?

There are generally two ways to store pictures in mysql: first, store the path of the picture to the database; second, 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:

References to the following namespaces:

Using System.Drawing; using System.IO; using System.Data.SqlClient

When designing a database, the corresponding field type in the table is iamge

Save:

/ / Image 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 (); / / save 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 ();

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

Thank you for reading this article carefully. I hope it is helpful for everyone to share the picture content in the mysql database. At the same time, I also hope you can support us, pay attention to the industry information channel, and find out if you have any problems. Detailed solutions are waiting for you to learn!

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