usually the images uploaded by the user need to be saved to the database. There are two general workarounds: one is to store the path of the picture saved to the database, and the other is to write the picture directly into the database field as a binary stream of data. The following are the specific methods:
first, save the image upload path to the database:
string Uppath= "";//for saving picture upload path
//Get the file name of the uploaded image
string filefullname = this. Fileupload1.filename;
//Get picture upload time, take time as the name of the picture can prevent picture duplicate
string dataname = DateTime.Now.ToString ("Yyyymmddhhmmss");
//Get the file name of the picture (without extension)
string fileName = Filefullname.substring (filefullname.lastindexof ("\ \") + 1);
//Get Picture extension
String type = Filefullname.substring (Filefullname.lastindexof (".") + 1);
//Determine if the format is required
if (type = = "BMP" | | type = = "JPG" | | type = = "JPEG" | | type = = "gif" | | type = = "JPG" | | type = = "JPEG" | | "BMP" | | Type = = "GIF")
{
//Upload the picture to a folder in the specified path
This . Fileupload1.saveas (Server.MapPath ("~/upload") + "\ \" + Dataname + "." + type);
//Save the path to a variable, save the value of the variable to the corresponding field in the database
Uppath = "~/upload/" + Dataname + "." + type;
}
second, the picture is stored in binary data stream directly to the database:
refer to the following namespaces:
using System.Drawing;
using System.IO;
using System.Data.SqlClient;
when you design a database, the corresponding field type in the table is Iamge
Save:
//Picture path
string strpath = this. FileUpload1.PostedFile.FileName.ToString ();
//Read pictures
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 ();
//Deposit
SqlConnection myconn = new SqlConnection ("Data source=.;i Nitial Catalog=stumanage; User Id=sa; Password=123 ");
string strcomm = "INSERT into Stuinfo (stuid,stuimage) VALUES (107, @photoBinary)";//Manipulate database statements to modify as needed
SqlCommand Mycomm = new SqlCommand (Strcomm, myconn);
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:
..... Connection database string omitted
Mycon. Open ();
SqlCommand command = new
SqlCommand ("Select Stuimage from Stuinfo where stuid=107", mycon);//query statements modify as needed
byte[] image = (byte[]) command. ExecuteScalar ();
//Specify the save path and name of the image read from the database
string strpath = "~/upload/zhangsan. JPG ";
string strphotopath = Server.MapPath (strpath);
//Save the picture file by the path and name above
BinaryWriter bw = new BinaryWriter (File.Open (strphotopath,filemode.openorcreate));
bw. Write (image);
bw. Close ();
//Show Pictures
This . Image1.imageurl = strpath;
two ways can be flexibly selected according to the actual needs.
Article Source: https://zhidao.baidu.com/question/1366429404998502979.html
How pictures are stored in the database