2010-11-29 51 views
0

在我的C#winform應用程序。我連接我的程序到MS SQL Server 2005成功,現在我想添加一個Image類型的新列,我該如何插入DB中的圖像並將其還原?並在我們的Business Logic Class哪個數據類型變量中我們會聲明?Ado.net圖像操作

回答

2

如何將圖像插入到數據庫中並將其取回?

你需要得到以字節爲單位的圖像如下圖所示代碼

編輯的代碼示例

private void BrowseImage(object o) 
    { 
     OpenFileDialog openFileDialog = new OpenFileDialog(); 

     // Set filter for file extension and default file extension 
     //openFileDialog.DefaultExt = ".bmp"; 
     //openFileDialog.Filter = "24-Bit Bitmap (.bmp)|*.bmp"; 
     openFileDialog.InitialDirectory = @"C://" 
     openFileDialog.DefaultExt = ".jpg"; 
     openFileDialog.Filter = 
      "BMP (*.BMP)|*.BMP|" + 
      "JPEG (*.JPG; *.JPEG; *.JPE)|*.JPG;*JPEG|" + 
      "GIF (*.GIF)|*.GIF|" + 
      "TIFF (*.TIFF)|*.TIFF|" + 
      "PNG (*.PNG)|*.PNG|" + 
      "DIB (*.DIB)|*.DIB|" + 
      "JFIF (*.JFIF)|*.JFIF"; 

     // Display OpenFileDialog by calling ShowDialog method 
     Nullable<bool> result = openFileDialog.ShowDialog(); 

     // Get the selected file name and display in a TextBox 
     if (result == true) 
     { 
      // Open document 
      string filename = openFileDialog.FileName; 


      Stream stream = openFileDialog.OpenFile(); 
      byte[] bytes = null; 
      if (stream != null && stream.CanRead) 
      { 

       bytes = new byte[stream.Length]; 
       stream.Read(bytes, 0, Convert.ToInt32(stream.Length)); 
       stream.Close(); 
      } 


     } 
    } 

在我們的業務邏輯類數據類型的變量,我們將宣佈?

您需要將image屬性定義爲Byte []並將此屬性賦予上面獲得的bytes []。

+0

流stream = ???你的意思是讓你更好地解釋它。 – salman 2010-11-29 07:34:39