2012-07-29 63 views
-1

我想使用DataBindign將圖片添加到數據庫,但我不知道該怎麼做。 這是我用來加載圖像的代碼:如何使用DataBindign將圖片添加到數據庫

byte [] imgData;

private void simpleButton5_Click_1(object sender, EventArgs e) 
    { 
     try 
     { 
      if (openFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       picture.ImageLocation = openFileDialog1.FileName; 

       imgData = File.ReadAllBytes(openFileDialog1.FileName); 
      } 
     } 
     catch (Exception ex) 
     { 
      // Could not load the image - probably related to Windows file system permissions. 
      XtraMessageBox.Show("Cannot display the image.\n You may not have permission to read the file, or " + 
       "it may be corrupt.\n\nReported error: " + ex.Message); 
     } 
    } 
+0

數據庫和數據綁定是不同的東西。你到底想知道什麼? – 2012-07-30 00:25:44

+0

例如,如果我想添加一個字符串,我使用這個: textbox1.DataBindings.Add(「text」,sql.ds.Tables [「E」],「NUMETU」); 如何添加圖片? – 2012-07-30 00:41:24

+0

但是你的數據不是來自數據庫。爲什麼你需要數據綁定?只需添加一個PictureBox並設置其圖像:PictureBox.Image = Image.FromFile(openFileDialog1.FileName) – 2012-07-30 00:45:17

回答

0

這裏是代碼如何從數據庫中讀取BLOB(在Oracle中)文件。我希望它能幫助你一點:

private void ReadFileFromDatabase() 
    { 
     byte[] fileData = null; 

     string selectSql = "SELECT FILE_DATA FROM BLOBTEST WHERE FILE_ID=1"; 

     OracleConnection con = new OracleConnection(conString); 
     OracleCommand cmd = new OracleCommand(selectSql, con); 

     try 
     { 
      con.Open(); 
      using (IDataReader reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        fileData = (byte[])reader["FILE_DATA"]; 
        //do your operations with fileData here 
       } 
      } 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 
相關問題