Get the App
SLTechnology News&Howtos  ›  Database  › 

Several methods of storing pictures in MySQL database

Shulou Source: shulou.com Published: 2022-06-01 03:05:43 10月02日 Update

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.

Tags: Picture data database path file content name field method binary variable extension data stream file name time statement learning value character string Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information Linux Shulou Tech Info Shulou Technology