C# 使用C将图片保存到数据库MySQL中的特定行中#

C# 使用C将图片保存到数据库MySQL中的特定行中#,c#,mysql,visual-studio,C#,Mysql,Visual Studio,我有一个演讲中的项目,用来制作将生物数据(学生ID、姓名、部门等)保存到数据库中的应用程序。我还想将图片配置文件保存到数据库MySQL中 以下是保存所有数据(图片除外)的功能: 如何在此方法中添加插入图片(即配置文件图片)的功能?类似于: string filename = Path.GetFileName(imageToSave.FileName); string fileExtension = Path.GetExtension(filename);

我有一个演讲中的项目,用来制作将生物数据(学生ID、姓名、部门等)保存到数据库中的应用程序。我还想将图片配置文件保存到数据库MySQL中

以下是保存所有数据(图片除外)的功能:

如何在此方法中添加插入图片(即配置文件图片)的功能?

类似于:

        string filename = Path.GetFileName(imageToSave.FileName);
        string fileExtension = Path.GetExtension(filename);
        int fileSize = imageToSave.ContentLength;

        if (fileExtension.ToLower() == ".jpg" ) /*you could add a check for what type of image you want to be allowed to save*/
        {
            Stream stream = postedFile.InputStream;
            BinaryReader binaryReader = new BinaryReader(stream);
            Byte[] bytes = binaryReader.ReadBytes((int)stream.Length);
         

    SqlParameter paramImageData = new SqlParameter()
    {
    ParameterName = "@ImageData",
    Value = bytes
    };
cmd.ExecuteNonQuery();
}

看一看,这只是另一个添加字节[]的参数。但是请注意,在代码中,您并没有列出列,而只是列出完全依赖于表结构的值。如果有人修改字段的顺序,如果幸运的话,您的代码将失败,或者会在错误的列中悄悄地插入值。
        string filename = Path.GetFileName(imageToSave.FileName);
        string fileExtension = Path.GetExtension(filename);
        int fileSize = imageToSave.ContentLength;

        if (fileExtension.ToLower() == ".jpg" ) /*you could add a check for what type of image you want to be allowed to save*/
        {
            Stream stream = postedFile.InputStream;
            BinaryReader binaryReader = new BinaryReader(stream);
            Byte[] bytes = binaryReader.ReadBytes((int)stream.Length);
         

    SqlParameter paramImageData = new SqlParameter()
    {
    ParameterName = "@ImageData",
    Value = bytes
    };
cmd.ExecuteNonQuery();
}