我正在尝试为用户创建一个删除页面,以删除相册中选定的图像文件。我只是不知道如何使用 url 查询字符串选择数据库表行“navn”(英文中的“name”)。
我应该为此行创建一个新的 SqlConnection 和 selectcommand 吗?
我不太确定该怎么做,因为我刚刚开始学习 ASP.NET、C# 和 SQL 的基础知识。
(我希望丹麦语条目不会太混乱。欢迎您询问是否有任何不妥之处。)
我想也许我应该在表格中Request.QueryString
选择“ navn ”行之后放一些东西。
File.Delete(Server.MapPath("~/upload/originals/" +
Request.QueryString["id"].ToString()));
Db 表“billeder”(英文为“images”):
- ID [整数]
- imgnavn [Nvarchar]
- thumbnavn [Nvarchar]
- alt [Nvarchar]
- opretettDen (创建日期) [DateTime]
- redigeretDen (上次更改) [日期时间]
下面是 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"]);
}
}