国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# 圖片保存到數據庫和從數據庫讀取圖片并顯示

2019-11-17 03:02:18
字體:
來源:轉載
供稿:網友
C# 圖片保存到數據庫和從數據庫讀取圖片并顯示

圖片保存到數據庫的方法:

public void imgToDB(string sql) { //參數sql中要求保存的imge變量名稱為@images //調用方法如:imgToDB("update UserPhoto setPhoto=@imageswhere UserNo='" + temp + "'"); FileStream fs = File.OpenRead(t_photo.Text); byte[] imageb = new byte[fs.Length]; fs.Read(imageb, 0, imageb.Length); fs.Close(); SqlCommand com3 = new SqlCommand (sql,con); com3.Parameters.Add("@images", SqlDbType.Image).Value = imageb; if (com3.Connection.State == ConnectionState.Closed) com3.Connection.Open(); try { com3.ExecuteNonQuery(); } catch { } finally { com3.Connection.Close(); } }

數據庫中讀出圖片并顯示在picturebox中:

方法一:PRivate void ShowImage(string sql) { //調用方法如:ShowImage("select Photo from UserPhoto where UserNo='" + userno +"'"); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); byte[] b= (byte[])cmd.ExecuteScalar(); if (b.Length 〉 0) { MemoryStream stream = new MemoryStream(b, true); stream.Write(b, 0, b.Length); pictureBox1.Image = new Bitmap(stream); stream.Close(); } conn.Close(); }

方法二:當在dg中選中某行時:private void dg_MouseUp(object sender, MouseEventArgs e) { //整行選擇 if (e.Button == System.Windows.Forms.MouseButtons.Left) {//用戶編號,姓名,性別,身份證號,籍貫,學院,系所,校區,部門,電話,照片 //顯示相片 object imgobj=dg[10, dg.CurrentRow.Index].Value; if (imgobj != null && !Convert.IsDBNull(imgobj)) { byte[] imgb = (byte[])imgobj; MemoryStream memStream = new MemoryStream(imgb); try { Bitmap myimge = new Bitmap(memStream); this.pictureBox1.Image = myimge; } catch { DB.msgbox("從數據庫讀取相片失?。?); } } else pictureBox1.Image = null; }

使用C#進行圖片的數據庫存取

本文總結如何在.Net WinForm和.Net WebForm(asp.net)中將圖片存入SQL Server中并讀取顯示的方法 。1.使用asp.net將圖片上傳并存入SQL Server中,然后從SQL Server中讀取并顯示出來:1)上傳并存入SQL Server

數據庫結構create table test{id identity(1,1),FImage image}相關的存儲過程Create proc UpdateImage(@UpdateImage Image)AsInsert Into test(FImage) values(@UpdateImage)GO在UpPhoto.aspx文件中添加如下:<input id="UpPhoto" name="UpPhoto" runat="server" type="file"><asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上傳"></asp:Button>然后在后置代碼文件UpPhoto.aspx.cs添加btnAdd按鈕的單擊事件處理代碼:private void btnAdd_Click(object sender, System.EventArgs e){//獲得圖象并把圖象轉換為byte[]HttpPostedFile upPhoto=UpPhoto.PostedFile;int upPhotoLength=upPhoto.ContentLength;byte[] PhotoArray=new Byte[upPhotoLength];Stream PhotoStream=upPhoto.InputStream;PhotoStream.Read(PhotoArray,0,upPhotoLength);//連接數據庫SqlConnection conn=new SqlConnection();conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";SqlCommand cmd=new SqlCommand("UpdateImage",conn);cmd.CommandType=CommandType.StoredProcedure;cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);cmd.Parameters["@UpdateImage"].Value=PhotoArray;//如果你希望不使用存儲過程來添加圖片把上面四句代碼改為://string strSql="Insert into test(FImage) values(@FImage)";//SqlCommand cmd=new SqlCommand(strSql,conn);//cmd.Parameters.Add("@FImage",SqlDbType.Image);//cmd.Parameters["@FImage"].Value=PhotoArray;conn.Open();cmd.ExecuteNonQuery();conn.Close();}2)從SQL Server中讀取并顯示出來在需要顯示圖片的地方添加如下代碼:<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>ShowPhoto.aspx主體代碼:private void Page_Load(object sender, System.EventArgs e){if(!Page.IsPostBack){SqlConnection conn=new SqlConnection()conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";string strSql="select * from test where id=2";//這里假設獲取id為2的圖片SqlCommand cmd=new SqlCommand(strSql,conn);conn.Open();SqlDataReader reader=cmd.ExecuteReader();reader.Read();Response.ContentType="application/octet-stream";Response.BinaryWrite((Byte[])reader["FImage"]);Response.End();reader.Close();}}

2.在WinForm中將圖片存入SQL Server,并從SQL Server中讀取并顯示在picturebox中1),存入SQL Server數據庫結構和使用的存儲過過程,同上面的一樣首先,在窗體中加一個OpenFileDialog控件,命名為ofdSelectPic ;然后,在窗體上添加一個打開文件按鈕,添加如下單擊事件代碼:Stream ms;byte[] picbyte;//ofdSelectPic.ShowDialog();if (ofdSelectPic.ShowDialog()==DialogResult.OK){if ((ms=ofdSelectPic.OpenFile())!=null){//MessageBox.Show("ok");picbyte=new byte[ms.Length];ms.Position=0;ms.Read(picbyte,0,Convert.ToInt32(ms.Length));//MessageBox.Show("讀取完畢!");//連接數據庫SqlConnection conn=new SqlConnection();conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";SqlCommand cmd=new SqlCommand("UpdateImage",conn);cmd.CommandType=CommandType.StoredProcedure;cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);cmd.Parameters["@UpdateImage"].Value=picbyte;conn.Open();cmd.ExecuteNonQuery();conn.Close();ms.Close();}}2)讀取并顯示在picturebox中首先,添加一個picturebox,名為ptbShow然后,添加一個按鈕,添加如下響應事件:SqlConnection conn=new SqlConnection();conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";string strSql="select FImage from test where id=1";SqlCommand cmd=new SqlCommand(strSql,conn);conn.Open();SqlDataReader reader=cmd.ExecuteReader();reader.Read();MemoryStream ms=new MemoryStream((byte[])reader["FImage"]);

Image image=Image.FromStream(ms,true);reader.Close();conn.Close();ptbShow.Image=image;


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安化县| 桦甸市| 禹州市| 册亨县| 丰宁| 抚顺市| 新龙县| 宿迁市| 宝山区| 济源市| 石柱| 新丰县| 昌黎县| 子长县| 台北县| 屏东县| 绵阳市| 平乡县| 宜昌市| 万宁市| 年辖:市辖区| 基隆市| 长汀县| 鱼台县| 昔阳县| 广昌县| 大新县| 桦甸市| 房产| 霍州市| 体育| 长沙县| 城步| 长武县| 普兰县| 玉林市| 岳阳县| 思茅市| 武胜县| 新邵县| 凌云县|