[英]Insert image into MS Access database using C#
我正在嘗試將圖像和名稱都輸入到我的ms Access數據庫中,但是我不知道該怎么做。 目前,我將輸入圖像轉換為字節格式的第一個功能。 秒功能是我將圖像添加到數據庫中的地方。 實際上,這段代碼來自教程,但是他們沒有告訴您如何將圖像插入MS Access數據庫。 有關功能的注釋已清楚解釋了代碼注釋。
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的數據庫部分。 非常感謝您的幫助。 謝謝。
我不喜歡使用“答案”來給出更適合作為注釋的內容,但是我想告訴您兩點。
首先,如果要將圖像存儲在數據庫中,則應查看BLOB(二進制大對象)。
但是第二點,也許更重要的是,似乎最佳實踐的共識是,如果您可以在項目中放棄它,則將圖像保存到其他位置並僅將路徑存儲在Access中。 這有助於使您保持在Access db大小上限以下,並且似乎也使數據庫具有更高的響應速度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.