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 将图像上传到服务器并将文件名存储


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01