欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

.NET在SQL Server中的圖片存取技術(shù)

系統(tǒng) 1909 0
本文總結(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 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ù)庫(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 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();
}
}

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;

.NET在SQL Server中的圖片存取技術(shù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国产精品视屏 | 九九色综合网 | 99久久婷婷 | 国产精品入口免费视频 | 日韩欧美视频一区二区在线观看 | 久久久久在线观看 | 丁香激情五月 | 亚洲欧美另类色妞网站 | 国产精品视频免费 | www.天天色.com | 久久久九九精品国产毛片A片 | 国内精品免费一区二区观看 | 日韩a级一片 | 91在线播放网站 | 亚洲激情视频网 | 国产99久久精品一区二区 | 江苏少妇性BBB搡BBB爽爽爽 | 国产精品不卡一区 | 久久草在线视频 | 黄色电影在线免费观看 | 国产免费小视频在线观看 | 欧美精品一区二区三区在线播放 | 国产片在线观看 | 一级激情片 | 欧美二区视频 | 欧美成人26uuu欧美毛片 | 亚洲一区二区三区高清 | 丁香五月亚洲综合在线 | 99pao成人国产永久免费视频 | 亚洲精品久久久 | 国产亚洲精品久久精品录音 | 在线播放av片 | 激情综合网五月 | 日韩一区不卡 | 91福利精品老师国产自产在线 | 久久免费看少妇高潮A片JA | 国产日韩欧美在线 | 国产自产拍精品视频免费看 | 日韩一区二区三区在线看 | av免费网站 | 国产精品亚洲精品青青青 |