能够存储图片。在mysql存储图片的要领平常有两种:其一,将图片保留的途径存储到数据库;其二,将图片以二进制数据流的情势直接写入数据库字段中。
一、保留图片的上传途径到数据库:
string uppath="";//用于保留图片上传途径 //猎取上传图片的文件名 string fileFullname = this.FileUpload1.FileName; //猎取图片上传的时候,以时候作为图片的名字能够防备图片重名 string dataName = DateTime.Now.ToString("yyyyMMddhhmmss"); //猎取图片的文件名(不含扩展名) string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1); //猎取图片扩展名 string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1); //推断是不是为请求的花样 if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF") { //将图片上传到指定途径的文件夹 this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type); //将途径保留到变量,将该变量的值保留到数据库响应字段即可 uppath = "~/upload/" + dataName + "." + type; }
二、将图片以二进制数据流直接保留到数据库:
援用以下定名空间:
using System.Drawing; using System.IO; using System.Data.SqlClient;
设想数据库时,表中响应的字段范例为iamge
保留:
//图片途径 string strPath = this.FileUpload1.PostedFile.FileName.ToString (); //读取图片 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(); //存入 SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123"); string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操纵数据库语句依据需要修正 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();
采纳这两种体式格局能够依据现实需求天真挑选。
以上就是mysql能够存储图片吗?的细致内容,更多请关注ki4网别的相干文章!