为什么这段代码只保存第一个图片,而我选择了更多的1
本文关键字:选择 段代码 代码 第一个 保存 为什么 | 更新日期: 2023-09-27 17:56:02
在上面的代码中,我用图片框选择了多个图像 时间选择并显示在面板中,面板显示所有选定的图像,但在数据库中只有第一张图片多次,例如我选择 10 张图片,在数据库中首先保存图片 10 次
if (files != null)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Avais'Desktop'GUI of GUA'GUI of GUA'GUAdatabase.mdf;Integrated Security=True");
con.Open();
FileStream fs = new FileStream(files[0], System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] image = new byte[fs.Length];
fs.Read(image, 0, Convert.ToInt32(fs.Length));
fs.Close();
SqlCommand cmd = new SqlCommand("INSERT INTO Admin_Pic_Lib(Pictures) VALUES (@pic)", con);
SqlParameter prm = new SqlParameter("@pic", SqlDbType.VarBinary, image.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, image);
cmd.Parameters.Add(prm);
cmd.ExecuteNonQuery();
label1.ForeColor = Color.Green;
label1.Text = "Pic Added Suscussfully";
con.Close();
}
您目前仅定位第一个可用的图像/文件:
FileStream fs = new FileStream(files[0], ...);
如果要定位并保存多个文件,请考虑遍历文件集合并为每个文件执行此操作:
foreach(var file in files)
{
// Read each individual file and save it here
}