亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

net在sql server中的圖片存取技術(例子在管理里inoutimg)

瀏覽:129日期:2023-11-03 13:25:06
本文總結如何在.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 ) As Insert Into test(FImage) values(@UpdateImage) GO 在UpPhoto.aspx文件中添加如下: <input name='UpPhoto' runat='server' type='file'> <asp:Button 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 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;
標簽: Sql Server 數據庫
主站蜘蛛池模板: 伊人久久青草青青综合 | 中文字幕性 | 免费看av在线网站网址 | 日本色黄视频 | 久久免费精品国产72精品剧情 | 香港三级做爰大爽视频 | 一级毛片免费高清视频 | 免费看的黄色大片 | 白丝丝袜高跟国产在线视频 | 亚洲第一二三四区 | 高清国产一级毛片国语 | 中文乱码一二三四有限公司 | 麻豆视频免费观看入口 | 一级特黄aaa大片免费看 | 日韩一区二区三区免费体验 | 国产精品久久久久久麻豆一区 | 精品国产一区二区三区不卡在线 | 国产精品嫩草影视在线观看 | 九九99热久久精品在线6手机 | 日韩精品高清自在线 | 国产成人成人一区二区 | 麻豆高清视频在线观看 | 草草影院一级毛片a级 | 亚洲图片综合区另类图片 | 亚洲精品第五页中文字幕 | 日本特黄特黄aaaaa大片 | 美女色影院 | 亚洲一区二区日韩欧美gif | 精品欧美一区二区三区在线 | 国产亚洲综合一区在线 | 国内精品一区二区在线观看 | 久久久国产精品免费 | 91最新入口| 国产欧美在线观看 | 亚洲国产天堂久久综合 | 国产看片一区二区三区 | 一级片在线免费看 | 人人草人人草 | 国产精品视频免费播放 | 亚洲国产综合网 | 亚洲高清免费视频 |