問題描述
我目前正在使用以下代碼段將數(shù)據(jù)插入到我的數(shù)據(jù)庫中的表中.它工作得很好.但是,我想開始添加文件名數(shù)據(jù),但不知道如何繼續(xù).
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.
我有以下幾點(diǎn):
// 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;
我還有一個文件上傳選項(xiàng).但是,我不知道如何使用它來執(zhí)行以下操作:
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 添加到我的表單中,但現(xiàn)在有點(diǎn)丟失.
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 文件夾中,應(yīng)該是:
To store the file in an images folder, it should be:
FileUpload1.SaveAs(Server.MapPath("~/Images/" + FileUpload1.FileName));
然后在文件名中添加命令參數(shù)
and then add the command parameters in the fileName
comm.Parameters["@FileName"].Value = FileUpload1.FileName;
注意:您的數(shù)據(jù)庫表中必須有 FileName
字段.
Note: you must have the FileName
field in your DB table.
這篇關(guān)于使用 C#/.NET 將圖像上傳到服務(wù)器并將文件名存儲在數(shù)據(jù)庫中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!