在数据库中保存多个文件 [英] Save multiple files in database
本文介绍了在数据库中保存多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C#2008
我在flowlayout面板中显示多个图像文件,现在我想将这些文件保存在数据库中.如何选择这些文件的名称?
C# 2008
I am showing multiple images files in flowlayout panel and now I want to save these files in database. How can I select names of these files?
public frmMultiSelectFile()
{
InitializeComponent();
InitializeOpenFileDialog();
}
private void InitializeOpenFileDialog()
{
// Set the file dialog to filter for graphics files.
this.openFileDialog1.Filter =
"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
"All files (*.*)|*.*";
// Allow the user to select multiple images.
this.openFileDialog1.Multiselect = true;
this.openFileDialog1.Title = "My Image Browser";
}
private void butSelect_Click(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
// Read the files
foreach (String file in openFileDialog1.FileNames)
{
// Create a PictureBox.
try
{
PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(file);
pb.Height = 100;
pb.Width = 115;
pb.Image = loadedImage;
pb.SizeMode=
PictureBoxSizeMode.StretchImage;
flowLayoutPanel1.Controls.Add(pb);
}
catch (Exception ex)
{
// Could not load the image - probably related to Windows file system permissions.
MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
+ ". You may not have permission to read the file, or " +
"it may be corrupt.\n\nReported error: " + ex.Message);
}
}
}
}
推荐答案
您应该创建一个List< string>作为表单级变量.然后在foreach循环中,将图片添加到面板中,将每个图片的路径另存为字符串,然后将其添加到列表中.这样,您将拥有用户选择的所有文件的列表,以供以后使用.
希望对您有帮助
You should create a List<string> as a form level variable. Then in your foreach loop where you add the pictures to the panel save the path of each one as a string, and add it to the List. That way you will have a list of all files selected by the user, to use later.
Hope this helps
这篇关于在数据库中保存多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文