兩種,
1、一種是將圖片轉化成二進制數據流存入資料庫中;
2、一種是保存圖片的路徑,然後前台讀取路徑去調用圖片;
特點:
相關的代碼網路一下應該會有,第二種方法實現上比較簡單,就是存儲路徑,然後根據路徑讀取對應的圖片顯示出來。第一種就比較麻煩,要先把圖片轉化成二進制數據,讀取時就是從資料庫讀取對應數據再轉化成圖片顯示出來。
圖片存儲緩存的話需要通過圖片轉化為數據流進行存放在資料庫裡面,調用的時候按照數據流找到圖片存放路徑轉化出來就可以。
❷ 圖片如何存入資料庫
1、新建一個資料庫,資料庫名為Image,表名為image。並為表添加ID,tupian兩個列。
❸ 如何將圖片儲存在Mysql資料庫里
解決方法一般有兩種:
1、將圖片保存的路徑存儲到資料庫;
2、將圖片以二進制數據流的形式直接寫入資料庫欄位中。
以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
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();
讀取:
...連接資料庫字元串省略
mycon.Open();
SqlCommand
command = new
SqlCommand("select stuimage from stuInfo where stuid=107",
mycon);//查詢語句根據需要修改
byte[] image = (byte[])command.ExecuteScalar
();
//指定從資料庫讀取出來的圖片的保存路徑及名字
string strPath =
"~/Upload/zhangsan.JPG";
string strPhotoPath =
Server.MapPath(strPath);
//按上面的路徑與名字保存圖片文件
BinaryWriter bw = new
BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//顯示圖片
this.Image1.ImageUrl
= strPath;
採用這兩種方式可以根據實際需求靈活選擇。
❹ VS中怎麼從資料庫讀取圖片
publicDataSetds=null;
publicSqlDataAdaptersda=null;
publicstaticSqlConnectionconn=null;
//連接資料庫
publicvoidOpenLink()
{
conn=newSqlConnection();
conn.ConnectionString="Server=192.168.1.5;uid=sa;pwd=555;dataBase=資料庫名";
try
{
conn.Open();
}
catch
{
MessageBox.Show("連接資料庫丟失!");
}
}
//查詢數據表
publicvoidlink(Stringsql)
{
if(conn!=null)
{
ds=newDataSet();
sda=newSqlDataAdapter();
sda.SelectCommand=newSqlCommand(sql,conn);
SqlCommandBuilderbuilder=newSqlCommandBuilder(sda);
sda.Fill(ds);
}
}
publicvoidPix(PictureBoxpx,Stringname)//顯示圖片
{
Stringsql="Select識別碼from下單表where訂票人='"+name+"'";
byte[]imagebytes=null;
SqlCommandcom=newSqlCommand(sql,conn);
SqlDataReaderdr=com.ExecuteReader();
while(dr.Read())
{
imagebytes=(byte[])dr.GetValue(0);
}
MemoryStreamms=newMemoryStream(imagebytes);
Bitmapbmpt=newBitmap(ms);
px.Image=bmpt;
px.SizeMode=PictureBoxSizeMode.StretchImage;
}
放工程啟動的時候就OpenLink(),把上面代碼存儲在類文件裡面,Pix
()函數就是顯示圖片。
❺ 如何用insert語句把插入image類型記錄到資料庫
如果非要寫sql語句如下,要將image轉化為二進制
INSERT INTO tbl VALUES (1,0xFFFFFFFF)
注意在寫入圖形信息前必須先將此資料庫的 'select into/bulk' 屬性設置為 True ,其語法如下:
use master
exec sp_dboption Im_Test ,'select into/bulk' ,True
public void InsertIMG()
{
//將需要存儲的圖片讀取為數據流
FileStream fs = new FileStream(@"D:\a.jpg", FileMode.Open,FileAccess.Read);
Byte[] b = new byte[fs.Length];
fs.Read(b , 0, Convert.ToInt32(fs.Length));
fs.Close();
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into tbl(imgfile) values(@imgfile)";
SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image);
par.Value = b;
cmd.Parameters.Add(par);
int t=(int)(cmd.ExecuteNonQuery());
if (t > 0)
{
Console.WriteLine("插入成功");
}
conn.Close();
}
}
❻ sql2005 image欄位如何添加照片
C#讀取Image數據類型:
(1)控制台應用程序下演示插入圖片
publicvoidInsertIMG()
{
//將需要存儲的圖片讀取為數據流
FileStreamfs=newFileStream(@"E:c.jpg",FileMode.Open,FileAccess.Read);
Byte[]btye2=newbyte[fs.Length];
fs.Read(btye2,0,Convert.ToInt32(fs.Length));
fs.Close();
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="insertintoT_Img(imgfile)values(@imgfile)";
SqlParameterpar=newSqlParameter("@imgfile",SqlDbType.Image);
par.Value=bt;
cmd.Parameters.Add(par);
intt=(int)(cmd.ExecuteNonQuery());
if(t>0)
{
Console.WriteLine("插入成功");
}
conn.Close();
}
}
(2)控制台應用程序下讀出並生成圖片到物理位置
publicvoidRead()
{
byte[]MyData=newbyte[0];
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="select*fromT_img";
SqlDataReadersdr=cmd.ExecuteReader();
sdr.Read();
MyData=(byte[])sdr["ImgFile"];//讀取第一個圖片的位流
intArraySize=MyData.GetUpperBound(0);//獲得資料庫中存儲的位流數組的維度上限,用作讀取流的上限
FileStreamfs=newFileStream(@"c: 0.jpg",FileMode.OpenOrCreate,FileAccess.Write);
fs.Write(MyData,0,ArraySize);
fs.Close();//--寫入到c: 0.jpg。
conn.Close();
Console.WriteLine("讀取成功");//查看硬碟上的文件
}
}
(3)Web下picshow.aspx頁將圖片讀取出來並寫入到瀏覽器上呈現
publicvoidRead()
{
byte[]MyData=newbyte[0];
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="select*fromT_img";
SqlDataReadersdr=cmd.ExecuteReader();
sdr.Read();
MyData=(byte[])sdr["ImgFile"];
Response.ContentType="image/gif";
Response.BinaryWrite(MyData);
conn.Close();
Response.Write("讀取成功");
}
(4)在web中可以如上picshow.aspx頁面讀取並顯示圖片,而真正引用該圖片時如下示例
<imgsrc="picshow.aspx"width="500"height="300"/>
(5)Winform下將圖片寫入到sql資料庫image類型欄位中的方法和以上方法基本一致,僅區別於可以利用多個對話框來幫助選取存儲圖片等,各個屬性可以方便的利用上
(6)Winform下讀取圖片在picturebox控制項中顯示出來
方法一:利用MemoryStream和System.Drawing.Image
publicvoidRead()
{
byte[]MyData=newbyte[0];
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="select*fromT_img";
SqlDataReadersdr=cmd.ExecuteReader();
sdr.Read();
MyData=(byte[])sdr["ImgFile"];
MemoryStreammystream=newMemoryStream(MyData);
//用指定的數據流來創建一個image圖片
System.Drawing.Imageimg=System.Drawing.Image.FromStream(mystream,true);
System.Windows.Forms.PictureBoxpicbox=newPictureBox();
picbox.Image=img;
picbox.Left=30;
picbox.Top=80;
picbox.Width=800;
picbox.Height=500;
this.Controls.Add(picbox);
mystream.Close();
conn.Close();
}
}
方法二:將流直接讀取成圖片並寫入到物理位置,然後再行利用該圖片呈現
voidRead()
{
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="select*fromT_img";
SqlDataReadersdr=cmd.ExecuteReader();
sdr.Read();
byte[]Image_img=(byte[])sdr["ImgFile"];
if(Image_img.Length==0)
{
return;
}
intfilelength=Image_img.Length;
stringimageName="1.jpg";
stringmyUrl=Environment.CurrentDirectory+"\"+imageName;
FileStreamfs=newFileStream(myUrl,FileMode.OpenOrCreate,FileAccess.Write);
BinaryWriterBW=newBinaryWriter(fs);
BW.BaseStream.Write(Image_img,0,filelength);
BW.Flush();
BW.Close();
System.Windows.Forms.PictureBoxpicbox=newPictureBox();
//為picbox添加圖片方法一
//picbox.ImageLocation=myUrl;
//picbox.Width=800;
//picbox.Height=300;
//為picbox添加圖片方法二
Bitmapbitmap=newBitmap(myUrl);
picbox.Width=100;//bitmap.Width;
picbox.Height=80;//bitmap.Height;
picbox.Image=(Image)bitmap;
picbox.SizeMode=System.Windows.Forms.PictureBoxSizeMode.StretchImage;
picbox.Left=20;
picbox.Top=30;
this.Controls.Add(picbox);
conn.Close();
}
}
❼ 圖片如何存入資料庫
通常對用戶上傳的圖片需要保存到資料庫中。解決方法一般有兩種:一種是將圖片保存的路徑存儲到資料庫;另一種是將圖片以二進制數據流的形式直接寫入資料庫欄位中。以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
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();
讀取:
...連接資料庫字元串省略
mycon.Open();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查詢語句根據需要修改
byte[] image = (byte[])command.ExecuteScalar ();
//指定從資料庫讀取出來的圖片的保存路徑及名字
string strPath = "~/Upload/zhangsan.JPG";
string strPhotoPath = Server.MapPath(strPath);
//按上面的路徑與名字保存圖片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//顯示圖片
this.Image1.ImageUrl = strPath;
採用倆種方式可以根據實際需求靈活選擇。
❽ C# 資料庫中image 讀取 參數無效
網頁鏈接這里有講到圖片轉換數組時的問題,應該對解決問題會有點幫助