本文總結如何在
一
數據庫結構
create table test
{
id identity(
FImage image
}
相關的存儲過程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO
在UpPhoto
<input id=
<asp:Button id=
然後在後置代碼文件UpPhoto
private void btnAdd_Click(object sender
{
//獲得圖象並把圖象轉換為byte[]
HttpPostedFile upPhoto=UpPhoto
int upPhotoLength=upPhoto
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto
PhotoStream
//連接數據庫
SqlConnection conn=new SqlConnection();
conn
SqlCommand cmd=new SqlCommand(
cmd
cmd
cmd
//如果你希望不使用存儲過程來添加圖片把上面四句代碼改為
//string strSql=
//SqlCommand cmd=new SqlCommand(strSql
//cmd
//cmd
conn
cmd
conn
}
二
在需要顯示圖片的地方添加如下代碼:
<asp:image id=
ShowPhoto
private void Page_Load(object sender
{
if(!Page
{
SqlConnection conn=new SqlConnection()
conn
string strSql=
SqlCommand cmd=new SqlCommand()
reader
Response
Response
Response
reader
}
}
數據庫結構和使用的存儲過過程
Stream ms;
byte[] picbyte;
//ofdSelectPic
if (ofdSelectPic
{
if ((ms=ofdSelectPic
{
//MessageBox
picbyte=new byte[ms
ms
ms
//MessageBox
//連接數據庫
SqlConnection conn=new SqlConnection();
conn
SqlCommand cmd=new SqlCommand(
cmd
cmd
cmd
conn
cmd
conn
ms
}
}
SqlConnection conn=new SqlConnection();
conn
string strSql=
SqlCommand cmd=new SqlCommand(strSql
conn
SqlDataReader reader=cmd
reader
MemoryStream ms=new MemoryStream((byte[])reader[
Image image=Image
reader
conn
ptbShow
From:http://tw.wingwit.com/Article/program/net/201311/12864.html