国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:132日期:2023-11-03 13:25:06
本文總結(jié)如何在.Net WinForm和.Net WebForm(asp.net)中將圖片存入SQL Server中并讀取顯示的方法 。1.使用asp.net將圖片上傳并存入SQL Server中,然后從SQL Server中讀取并顯示出來(lái):1)上傳并存入SQL Server 數(shù)據(jù)庫(kù)結(jié)構(gòu) create table test { id identity(1,1), FImage image } 相關(guān)的存儲(chǔ)過(guò)程 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) { //獲得圖象并把圖象轉(zhuǎn)換為byte[] HttpPostedFile upPhoto=UpPhoto.PostedFile; int upPhotoLength=upPhoto.ContentLength; byte[] PhotoArray=new Byte[upPhotoLength]; Stream PhotoStream=upPhoto.InputStream; PhotoStream.Read(PhotoArray,0,upPhotoLength); //連接數(shù)據(jù)庫(kù) 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; //如果你希望不使用存儲(chǔ)過(guò)程來(lái)添加圖片把上面四句代碼改為: //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中讀取并顯示出來(lái) 在需要顯示圖片的地方添加如下代碼: <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';//這里假設(shè)獲取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 數(shù)據(jù)庫(kù)結(jié)構(gòu)和使用的存儲(chǔ)過(guò)過(guò)程,同上面的一樣 首先,在窗體中加一個(gè)OpenFileDialog控件,命名為ofdSelectPic ;然后,在窗體上添加一個(gè)打開(kāi)文件按鈕,添加如下單擊事件代碼: 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('讀取完畢!'); //連接數(shù)據(jù)庫(kù) 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中 首先,添加一個(gè)picturebox,名為ptbShow 然后,添加一個(gè)按鈕,添加如下響應(yīng)事件: 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;
主站蜘蛛池模板: 亚洲精品一区二区三区五区 | 欧美成人亚洲 | 欧美日本一区二区三区 | 国产精品亚洲精品不卡 | 久草视频在线免费播放 | 免费看a级肉片 | 一级毛片一级毛片a毛片欧美 | 综合九九 | 97久久曰曰久久久 | 岛国片欧美一级毛片 | 91桃色成人免费 | 日本三级香港三级网站 | 那里有黄色网址 | 国产欧美一区二区三区精品 | 久久久久久久国产精品影院 | 三级视频在线观看 | 精品久久久久久综合网 | 日韩欧美亚洲国产 | 国产成人深夜福利在线观看 | 一区二区三区欧美在线 | 九九re6精品视频在线观看 | 久久久日本久久久久123 | 99毛片| 免费观看性欧美一级 | 男人的天堂中文字幕 | 亚洲视频99| 精品久久久久久综合网 | 日黄网站 | 午夜影院美女 | 亚洲欧美卡通成人制服动漫 | 日本一级特大毛片 | 性欧美在线 | 国产成人精品福利网站在线 | 久久99热精品免费观看欧美 | 热99re久久国超精品首页 | 国产人成久久久精品 | xxxwww黄色| 日韩亚洲欧美一区噜噜噜 | 香蕉超级碰碰碰97视频在线观看 | 久久精品视频2 | 欧美特一级 |