如何在 C# windows 应用程序的图片框中从 SQL 服务器数据库检索多个图像?

How to retrieve multiple images from SQL Server database in picturebox in C# windows application?

我正在尝试使用 c# windows 应用程序中的以下代码,它可以很好地从 SQL 服务器数据库 table.

中检索单个图像
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace ManagementSystem
{
    public partial class frmSearchResult : Form
    {
        public frmSearchResult()
        {
            InitializeComponent();
        }

        SqlConnection con;
        SqlCommand cmd;

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            con = new SqlConnection("Data Source=.\sqlexpress;Initial Catalog=managementsystem;Integrated Security=True");
            con.Open();

            cmd = new SqlCommand("Select M_Photo, S_Photo From Members where M_Number=15", con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();

            da.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["M_Photo"]);
                pic_M.Image = new Bitmap(ms);
            }
        }
    }
}

但我需要从数据库中检索多张图片。当我在末尾附加以下代码时,出现错误:

Parameter is not valid.

附加代码是

MemoryStream ms1 = new MemoryStream((byte[])ds.Tables[0].Rows[0]["S_Photo"]);
pic_S.Image = new Bitmap(ms1);

这有什么问题?请帮忙。

谢谢!

主要问题在于以正确的方式保存图像。对每个图像使用下面的代码将图像保存在数据库图像字段中,并使用上面有问题的代码来检索图像。

//Convert image to binary
string imgpathC2 = txtH_C2.Text;
FileStream fsC2;
fsC2 = new FileStream(@imgpathC2, FileMode.Open, FileAccess.Read);
byte[] picbyteC2 = new byte[fsC2.Length];
fsC2.Read(picbyteC2, 0, System.Convert.ToInt32(fsC2.Length));
fsC2.Close();

//Add binary value to SQL parameter
SqlParameter picparaC2 = new SqlParameter();
picparaC2.SqlDbType = SqlDbType.Image;
picparaC2.ParameterName = "picC2";
picparaC2.Value = picbyteC2;

//use parameter in command
cmd.Parameters.Add(picparaC2);

谢谢!