廣告贊助

以下為一個上傳圖片到 SQL Server 範例,點選 Button1 上傳圖片到資料庫,點選 Button2 顯示資料庫圖片。

 

環境:

Visual Studio 2010

.NET Framework 4

 

程式畫面:畫面中加入一個 PictureBox ( SizeMode 屬性改為 Zoom ) 跟兩個Button

 

資料庫:

Create DATABASE Test

GO

USE Test

GO

CREATE TABLE [dbo].[ImageData](

[Image_No] [varchar](10) NOT NULL,

[Image_Data] [image] NULL,

CONSTRAINT [PK_Image] PRIMARY KEY CLUSTERED

(

[Image_No] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

 

加入新增項目:

SETP 1. 加入新增項目 -> 新增ADO.NET 實體資料模型

 

 

SETP 2. 選擇從資料庫產生,點選下一步。

 

 

SETP 3. 點選新增連線按鈕建立資料庫連線後,勾選 【將實體連接設定另存在 App.Config中 】,點選下一步。

 

 

SETP 4.勾選資料表後,點選完成

 

 

ADO.NET 實體資料模型建立完成

程式:

 

public partial class Form1 : Form

   {

       public Form1()

       {

           InitializeComponent();

        

       }

 

       private void button1_Click(object sender, EventArgs e)

       {

           //取得上傳圖片檔案

           MemoryStream ms = new MemoryStream();

           OpenFileDialog openFileDialog1 = new OpenFileDialog();

           Image img;

           byte[] arrImage = null;

           if (openFileDialog1.ShowDialog() == DialogResult.OK)

           {

               img = Image.FromFile(openFileDialog1.FileName);

               img.Save(ms, ImageFormat.Jpeg);

               arrImage = ms.GetBuffer();

               ms.Close();

 

           }

 

           //資料庫中如已有圖片則先刪除後新增

           try{

 

               using (var context = new TestEntities())

               {

 

                   var data = (from d1 in context.ImageData

                              where d1.Image_No == "1"

                               select d1).FirstOrDefault();

                  

                   if (data.Image_Data.Count() > 0)

                   {

                       context.DeleteObject(data);

                       context.SaveChanges();

                   }

               }

 

               using (var context = new TestEntities())

               {

                   var data = new ImageData

                   {

                       Image_No = "1",

                       Image_Data = arrImage

 

                   };

                   context.AddToImageData(data);

                   context.SaveChanges();

               }

           }

           catch (Exception ex)

           {

               MessageBox.Show(ex.Message, "錯誤");

           }

       }

 

       private void button2_Click(object sender, EventArgs e)

       {

           //從資料庫中抓出圖片,顯示在 Picture Box 元件中

           MemoryStream ms;

           byte[] arrayImage;

           using (var context = new TestEntities())

           {

               var d1 = context.ImageData;

               var data = from d in d1

                          where d.Image_No == "1"

                          select d;

               List<ImageData> p = data.ToList<ImageData>();

               if (p.Count > 0)

               {

                   arrayImage = p[0].Image_Data;

                   ms = new MemoryStream(arrayImage);

                   pictureBox1.Image = new Bitmap(Image.FromStream(ms));

               }

              

           }

       }

   }

 

創作者介紹
創作者 iammic 的頭像
iammic

簡單過生活

iammic 發表在 痞客邦 留言(0) 人氣()