我正在尝试为用户创建一个删除页面,以删除photoalbum中的选定图像文件。我只是不知道如何使用url querystring选择数据库表行“navn”(英文“name”)。
我应该为这一行创建一个新的SqlConnection并选择命令吗?
我不确定,该做什么,因为我刚开始学习ASP.NET,C#和SQL的基础知识。
(我希望丹麦语条目不会太混乱。欢迎你问一些事情是否有意义。)
我想也许我应该在 Request.QueryString
之后添加一些内容来选择表中的“ navn ” - 行。
File.Delete(Server.MapPath("~/upload/originals/" +
Request.QueryString["id"].ToString()));
Db表“billeder”( “images” ,英文):
以下是aspx页面和代码隐藏文件的完整代码:
(“sletBtn”是“删除按钮”)
sletBilleder.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/admin/AdminMasterPage.master" AutoEventWireup="true" CodeFile="SletBilleder.aspx.cs" Inherits="admin_SletBilleder" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head2" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
<table>
<tr>
<th>
<label>Billede-ID</label>
</th>
<th>
<label>Billede-navn</label>
</th>
</tr>
<tr>
<td>
<asp:Label ID="billedeID" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="billedenavn" runat="server"></asp:Label>
</td>
</tr>
</table>
<asp:Label ID="Label_msg" runat="server"></asp:Label>
<asp:Button ID="sletBtn" runat="server" Text="Slet billedet"
OnClick="sletBtn_Click" />
<asp:Button ID="tilAlbumBtn" runat="server" Text="Gå tilbage"
OnClick="tilAlbumBtn_Click" />
</asp:Content>
sletBilleder.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class admin_SletBilleder : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label_msg.Text = "<div class='span6 offset1'><div class='alert alert-success'>Er du sikker på, at du vil slette billedet</div></div>";
}
// opret forbindelse til databasen
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
// SQL strengen
cmd.CommandText = "SELECT * FROM [billeder] WHERE [Id] = @Id";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Request.QueryString["id"];
//åben for forbindelsen til databasen
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
billedeID.Text = reader["Id"].ToString();
billedenavn.Text = reader["imgnavn"].ToString();
}
// Luk for forbindelsen til databasen
// reader.Close();
conn.Close();
}
protected void sletBtn_Click(object sender, EventArgs e)
{
try
{
// slet Originalfilen i image/upload/Original/ mappen
File.Delete(Server.MapPath("~/upload/originals/" + Request.QueryString["id"].ToString()));
// Slet Thumbsfilen i /Images/Upload/Thumbs/
File.Delete(Server.MapPath("~/upload/thumbs/" + Request.QueryString["id"].ToString()));
// Slet Resized_Original i /Images/Upload/Resized_Original/ mappen
File.Delete(Server.MapPath("~/upload/originalsResized/" + Request.QueryString["id"].ToString()));
// slet filen i databasen
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM Billeder WHERE Id = @ImageId";
cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = Request.QueryString["id"].ToString();
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
//Redirect til Galleri
Response.Redirect("default.aspx");
}
catch (Exception)
{
Label_msg.Text = "<div class='span6 offset1'><div class='alert alert-success'>Billedet blev desværre <b>ikke</b> slettet</div></div>";
}
}
protected void tilAlbumBtn_Click(object sender, EventArgs e)
{
Response.Redirect("visBilleder.aspx?id=" + Request.QueryString["id"]);
}
}
答案 0 :(得分:0)
您可以尝试使用以下代码:
而不是使用
File.Delete(Server.MapPath("~/upload/originals/" + Request.QueryString["id"].ToString()));
你能用吗
File.Delete(Server.MapPath("~/upload/originals/") + Request.QueryString["id"].ToString());
如果您仍然遇到问题,可以尝试将数据放入变量并查看路径是否正常。我想知道你是否错过了文件扩展名?
string path = Server.MapPath("~/upload/originals/") + Request.QueryString["id"].ToString();
设置断点并查看路径的内容。
答案 1 :(得分:0)
您正在删除ID为以下名称且不存在的文件。
您需要删除具有正确文件名的文件。我没有测试代码,但我希望你能得到这个想法。
以下代码将文件名保存在ViewState中。然后单击一次按钮,从ViewState中检索该文件名。
protected void Page_Load(object sender, EventArgs e)
{
....
if (reader.Read())
{
billedeID.Text = reader["Id"].ToString();
billedenavn.Text = reader["imgnavn"].ToString();
ViewState["imgnavn"] = reader["imgnavn"].ToString();
}
....
}
protected void sletBtn_Click(object sender, EventArgs e)
{
try
{
string imageName = ViewState["imgnavn"].ToString();
// slet Originalfilen i image/upload/Original/ mappen
File.Delete(Server.MapPath("~/upload/originals/" + imageName));
// Slet Thumbsfilen i /Images/Upload/Thumbs/
File.Delete(Server.MapPath("~/upload/thumbs/" + imageName));
// Slet Resized_Original i /Images/Upload/Resized_Original/ mappen
File.Delete(Server.MapPath("~/upload/originalsResized/" + imageName));
...
}
...
}