熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.Net在SqlServer中的圖片存取

2013-11-13 10:11:45  來源: .NET編程 

  本文總結如何在Net Winform和Net webform()中將圖片存入sqlserver中並讀取顯示的方法

  使用將圖片上傳並存入SqlServer中然後從SqlServer中讀取並顯示出來

  一上傳並存入SqlServer

  數據庫結構

  create table test

  {

  id identity()

  FImage image

  }

  相關的存儲過程

  Create proc UpdateImage

  (

  @UpdateImage Image

  )

  As

  Insert Into test(FImage) values(@UpdateImage)

  GO

  在UpPhotoaspx文件中添加如下:

  <input id=UpPhoto name=UpPhoto runat=server type=file>

  <asp:Button id=btnAdd name=btnAdd runat=server Text=上傳></asp:Button>

  然後在後置代碼文件UpPhotoaspxcs添加btnAdd按鈕的單擊事件處理代碼:

  private void btnAdd_Click(object sender SystemEventArgs e)

  {

  //獲得圖象並把圖象轉換為byte[]

  HttpPostedFile upPhoto=UpPhotoPostedFile;

  int upPhotoLength=upPhotoContentLength;

  byte[] PhotoArray=new Byte[upPhotoLength];

  Stream PhotoStream=upPhotoInputStream;

  PhotoStreamRead(PhotoArrayupPhotoLength);

  //連接數據庫

  SqlConnection conn=new SqlConnection();

  connConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa;

  SqlCommand cmd=new SqlCommand(UpdateImageconn);

  cmdCommandType=CommandTypeStoredProcedure;

  cmdParametersAdd(@UpdateImageSqlDbTypeImage);

  cmdParameters[@UpdateImage]Value=PhotoArray;

  //如果你希望不使用存儲過程來添加圖片把上面四句代碼改為

  //string strSql=Insert into test(FImage) values(@FImage);

  //SqlCommand cmd=new SqlCommand(strSqlconn);

  //cmdParametersAdd(@FImageSqlDbTypeImage);

  //cmdParameters[@FImage]Value=PhotoArray;

  connOpen();

  cmdExecuteNonQuery();

  connClose();

  }

  二從SqlServer中讀取並顯示出來

  在需要顯示圖片的地方添加如下代碼:

  <asp:image id=imgPhoto runat=server ImageUrl=ShowPhotoaspx></asp:image>

  ShowPhotoaspx主體代碼

  private void Page_Load(object sender SystemEventArgs e)

  {

  if(!PageIsPostBack)

  {

  SqlConnection conn=new SqlConnection()

  connConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa;

  string strSql=select * from test where id=;//這裡假設獲取id為的圖片

  SqlCommand cmd=new SqlCommand()

  readerRead();

  ResponseContentType=application/octetstream;

  ResponseBinaryWrite((Byte[])reader[FImage]);

  ResponseEnd();

  readerClose();

  }

  }

  在winform中將圖片存入sqlserver並從sqlserver中讀取並顯示在picturebox中

  存入sqlserver

  數據庫結構和使用的存儲過過程同上面的一樣

  在窗體中加一個OpenFileDialog控件命名為ofdSelectPic

  在窗體上添加一個打開文件按鈕添加如下單擊事件代碼

  Stream ms;

  byte[] picbyte;

  //ofdSelectPicShowDialog();

  if (ofdSelectPicShowDialog()==DialogResultOK)

  {

  if ((ms=ofdSelectPicOpenFile())!=null)

  {

  //MessageBoxShow(ok);

  picbyte=new byte[msLength];

  msPosition=;

  msRead(picbyteConvertToInt(msLength));

  //MessageBoxShow(讀取完畢!);

  //連接數據庫

  SqlConnection conn=new SqlConnection();

  connConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa;

  SqlCommand cmd=new SqlCommand(UpdateImageconn);

  cmdCommandType=CommandTypeStoredProcedure;

  cmdParametersAdd(@UpdateImageSqlDbTypeImage);

  cmdParameters[@UpdateImage]Value=picbyte;

  connOpen();

  cmdExecuteNonQuery();

  connClose();

  msClose();

  }

  }

  讀取並顯示在picturebox中

   添加一個picturebox名為ptbShow

   添加一個按鈕添加如下響應事件

  SqlConnection conn=new SqlConnection();

  connConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa;

  string strSql=select FImage from test where id=;

  SqlCommand cmd=new SqlCommand(strSqlconn);

  connOpen();

  SqlDataReader reader=cmdExecuteReader();

  readerRead();

  MemoryStream ms=new MemoryStream((byte[])reader[FImage]);

  Image image=ImageFromStream(mstrue);

  readerClose();

  connClose();

  ptbShowImage=image;


From:http://tw.wingwit.com/Article/program/net/201311/12864.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.