我使用了foreach循环来读取多个图像文件,但我只能流式传输第一个选择的文件。当我尝试保存多个不同的图像时,输出的结果就像第一个选择的图像的副本一样,而不是其他不同的图像。
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(DBHandler.GetConnectionString());
try
{
OpenFileDialog fop = new OpenFileDialog();
fop.Multiselect = true;
fop.InitialDirectory = "C:\\";
fop.Filter = "JPG,JPEG|*.jpg|PNG|*png";
if (fop.ShowDialog() == DialogResult.OK)
{
foreach (String files in fop.FileNames)
{
FileStream FS = new FileStream(@fop.FileName, FileMode.Open, FileAccess.Read);
byte[] img = new byte[FS.Length];
FS.Read(img, 0, Convert.ToInt32(FS.Length));
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd = new SqlCommand("SaveImage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@img", SqlDbType.Image).Value = img;
cmd.ExecuteNonQuery();
}
MessageBox.Show("Image has been saved successfully!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
我希望在同一个表格中查看所有图像。
我的期望是:A-B-C-D (每个字母代表不同检索到的图像。“A”是从对话框中选择的第一张图像) 实际输出是:A-A-A-A。为什么会这样?