Upload image to server using C#/.NET and storing filename in DB(使用 C#/.NET 将图像上传到服务器并将文件名存储在数据库中)
问题描述
我目前正在使用以下代码段将数据插入到我的数据库中的表中.它工作得很好.但是,我想开始添加文件名数据,但不知道如何继续.
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:
- 将文件移动到我的 images 目录和
- 将 filename 值存储在我的表中.
- move the file to my images directory and
- store the filename value in my table.
我已将正确的 enctype 添加到我的表单中,但现在有点丢失.
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.
推荐答案
要将文件存储在 images 文件夹中,应该是:
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;
注意:您的数据库表中必须有 FileName
字段.
Note: you must have the FileName
field in your DB table.
这篇关于使用 C#/.NET 将图像上传到服务器并将文件名存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 C#/.NET 将图像上传到服务器并将文件名存储


- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01