成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

如何使用.NET向SQLServer數(shù)據(jù)庫存取圖片

這篇文章主要介紹“如何使用.NET向SQL Server數(shù)據(jù)庫存取圖片”,在日常操作中,相信很多人在如何使用.NET向SQL Server數(shù)據(jù)庫存取圖片問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何使用.NET向SQL Server數(shù)據(jù)庫存取圖片”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián),專注為中小企業(yè)提供官網(wǎng)建設(shè)、營銷型網(wǎng)站制作、響應(yīng)式網(wǎng)站設(shè)計、展示型成都網(wǎng)站制作、做網(wǎng)站等服務(wù),幫助中小企業(yè)通過網(wǎng)站體現(xiàn)價值、有效益。幫助企業(yè)快速建站、解決網(wǎng)站建設(shè)與網(wǎng)站營銷推廣問題。

使用.NET向SQL Server數(shù)據(jù)庫存取圖片

使用.Net技術(shù),我們可以很方便的將圖片存入SQL Server數(shù)據(jù)庫中并方便的讀取顯示出來,詳細(xì)的實現(xiàn)方法我們一步一步將會了解到。先說如何將圖片存儲到sql server數(shù)據(jù)庫中:

.NET向SQL Server數(shù)據(jù)庫存取圖片技巧:存入圖片

使用asp.net將圖片上傳并存入SQL Server中,然后從SQL Server中讀取并顯示出來:

1)上傳并存入SQL Server

數(shù)據(jù)庫結(jié)構(gòu)

create table test   {   id identity(1,1),   FImage image   }

相關(guān)的存儲過程

Create proc UpdateImage   (   @UpdateImage Image   )   As   Insert 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)   {   //獲得圖象并把圖象轉(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ù)庫   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();   }

.NET向SQL Server數(shù)據(jù)庫存取圖片技巧:從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";//這里假設(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();   }   }

到此,關(guān)于“如何使用.NET向SQL Server數(shù)據(jù)庫存取圖片”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

網(wǎng)站名稱:如何使用.NET向SQLServer數(shù)據(jù)庫存取圖片
網(wǎng)站網(wǎng)址:http://jinyejixie.com/article48/ggcphp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站內(nèi)鏈域名注冊、用戶體驗、手機網(wǎng)站建設(shè)、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)