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

Several methods of storing pictures in MySQL database

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

Share

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

Usually, the pictures uploaded by users need to be saved to the database.

There are generally two solutions:

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.

Here are the specific methods:

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

String uppath= ""; / / used to save the image upload path / / get the file name of the uploaded picture string fileFullname = this.FileUpload1.FileName; / / get the time of the picture upload. 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 (excluding the 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:

When using System.Drawing; using System.IO; using System.Data.SqlClient; designs the database, the corresponding field type in the table is iamge save: / / 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 grammar photobinary)"; / / modify the database statement 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 above path and name.

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.

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