方式一:
數據庫用的是SQL 2008,數據表中存放的是圖片的二進制數據,現在把圖片以一種圖片格式(如.jpg)導出,然后存放於指定的文件夾中,實現方式如下:
byte[] bytImg = (byte[])myDAL.DbHelperSQL.Query("SELECT F_Photo FROM myTable WHERE ID=1").Tables[0].Rows[0][0]; if (bytImg != null) { MemoryStream ms = new MemoryStream(bytImg); Image img = Image.FromStream(ms); img.Save("D:\\me.jpg"); }
方式二:
是windowform程序,數據庫已經建好,圖像以二進制形式存放在數據庫的image表中,我想把符合查詢條件的圖像(大量)從數據庫中讀出,顯示在form窗體上的一個控件(listview或imagelist還是picturebox?這個不知道那個合適),並保存到選擇(或新建)的一個文件夾中
SqlDataAdapter da = new SqlDataAdapter("select * from newpicture", conn);//數據庫連接,修改一下數據庫的操作。 DataSet ds = new DataSet(); da.Fill(ds, "pic");//將符合條件的選項保存在數據集的pic表里 string picdotname; string picfilename; int piclength; int i; //添加新列 DataColumn newcolumn = ds.Tables["pic"].Columns.Add("pic_url", typeof(string));//給pic表添加新的一列pic_url,保存你的新寫出的圖片路徑 for (i = 0; i < Convert.ToInt16(ds.Tables["pic"].Rows.Count); i++) { picdotname = ds.Tables["pic"].Rows[i]["pic_dot"].ToString();//圖片的拓展名,你數據庫要有這一列,如jpg piclength = Convert.ToInt32(ds.Tables["pic"].Rows[i]["pic_length"]);//數據流的長度 picfilename = Server.MapPath("新建的文件夾名/") + "添加圖片名"+ "." + picdotname; FileStream fs = new FileStream(picfilename, FileMode.Create, FileAccess.Write); byte[] piccontent = new byte[piclength]; piccontent = (byte[])ds.Tables["pic"].Rows[i]["pic_content"]; fs.Write(piccontent, 0, piclength); fs.Close();//讀出數據流寫成圖片 //最后把表綁定到控件上。 ds.Tables["pic"].Rows[i]["pic_url"] = "temp/temp" + i.ToString() + "." + picdotname;//意思給表pic的第i行,pic_url列里添加文件的路徑值。 } //數據源 = ds.Tables["pic"];//數據綁定
大體是這樣吧,里面表名列名很多細節你按你的表修改吧!