2015-08-08 103 views
0

我想圖像和名稱到我的MS訪問數據庫,但我不知道如何做到這一點。目前,我把我的輸入圖像轉換爲字節格式的第一個功能。秒功能是我的圖像添加到我的數據庫。實際上這個代碼來自教程,但他們沒有告訴如何將圖像插入MS Access數據庫。代碼的評論已經清楚地解釋了這些功能。插入圖像到MS Access數據庫使用C#

private byte[] ConvertToDBFormat(Image InputImage) 
    { 
     Bitmap BmpImage = new Bitmap(InputImage); 
     System.IO.MemoryStream Mystream = new System.IO.MemoryStream(); 
     BmpImage.Save(Mystream, System.Drawing.Imaging.ImageFormat.Jpeg); 
     byte[] ImageAsBytes = Mystream.ToArray(); 
     return ImageAsBytes; 

    } 
    /// <summary> 
    /// Stores a Face image and its Name in the Training Set, in MS Access Database 
    /// </summary> 
    /// <param name="ImageAsBytes"></param> Face image converted to bytes 
    /// <param name="FaceName"></param>the name of face set in the textbox 
    private void AddFaceToDB(Image InputFace, string FaceName) 
    { 
     if (Connection.State.Equals(ConnectionState.Closed)) 
     { 
      Connection.Open(); 
     } 

     try 
     { 
      MessageBox.Show("Image saved at: " + rowNumber.ToString()); 
      OleDbCommand OledbInsert = new OleDbCommand("Insert INTO TrainingSet1 (FaceID, FaceName, FaceImage) VALUES('" + rowNumber.ToString() + "','" + txtBoxFaceName.Text + "',@MyImg)", Connection); 
      OleDbParameter imageParameter = OledbInsert.Parameters.AddWithValue("@Img", SqlDbType.Binary); 

     } 

    } 

有人可以幫助我告訴我如何使用C#添加圖像和文本。這是我第一次學習c#和MS Access數據庫。目前,我不知道如何從這裏繼續使用我的代碼。我使用OleDb的數據庫部分。我非常感謝你的幫助。謝謝。

回答

0

我不喜歡用「答案」來給出更適合作爲評論的東西,但我想告訴你兩件事。

首先,如果要將圖像存儲在數據庫中,應查看BLOB(二進制大對象)。

但其次,也許更重要的是,似乎最佳實踐的共識是,如果您可以在項目中擺脫它,將圖像保存到其他地方並僅在Access中存儲路徑。這有助於讓你保持在Access數據庫大小上限下,並且似乎也可以使數據庫更具響應性。

+0

這實際上不是我的問題... – anonymous5671