使用C#将图像插入MS Access数据库

时间:2015-08-08 02:13:26

标签: c# database ms-access-2013

我正在尝试将图像和名称都写入我的ms访问数据库,但我不知道该怎么做。目前,我将输入图像转换为字节格式的第一个功能。秒函数是我将图像添加到数据库的地方。实际上这段代码来自一个教程,但他们没有说明如何将图像插入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。我非常感谢你的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我讨厌使用“答案”来提供更适合作为评论的内容,但我想告诉你两件事。

首先,如果要将图像存储在数据库中,则应该查看BLOB(二进制大对象)。

但第二个也许更重要的是,似乎对最佳实践的共识是,如果你可以在你的项目中侥幸使用它,将图像保存在其他地方并仅在Access中存储路径。这有助于使您保持在访问数据库大小上限之下,并且似乎也使数据库更具响应性。

相关问题