使用C#/。NET和DB存储的文件名上传图片到服务器

问题描述:

我目前使用下面的代码片断将数据插入到我的数据库中的表。它的伟大工程。但是,我要开始添加文件名数据和不知道如何着手。

I'm currently using the following snippet to insert data into a table in my database. It works great. But, I want to start adding filename data and not sure how to proceed.

我有以下内容:

// Create command 
comm = new SqlCommand(
  "INSERT INTO Entries (Title, Description) " +
  "VALUES (@Title, @Description)", conn);

// Add command parameters
comm.Parameters.Add("@Description", System.Data.SqlDbType.Text);
comm.Parameters["@Description"].Value = descriptionTextBox.Text;
comm.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["@Title"].Value = titleTextBox.Text;

我也有一个的文件上传的选项。但是,我不知道怎么用这个来做到以下几点:

I also have a File Upload option. But, I don't know how to use this to do the following:


  • 将文件移动到我的图片的目录,

  • 存储在我的表中的文件名的值。

  • move the file to my images directory and
  • store the filename value in my table.

我加入了正确的加密类型,以我的形式,但现在有点失落。

I have added the correct enctype to my form but now a little lost.

有人能解释这样做的最佳方式?

Can someone explain the best way to do this?

对于任何帮助,非常感谢。

Many thanks for any help with this.

要存储在图像文件夹中,它应该是:

To store the file in an images folder, it should be:

FileUpload1.SaveAs(Server.MapPath("~/Images/" + FileUpload1.FileName));

然后添加在文件名中的命令参数

and then add the command parameters in the fileName

comm.Parameters["@FileName"].Value = FileUpload1.FileName;

请注意:你必须在你的数据库表中的文件名字段

Note: you must have the FileName field in your DB table.