简体   繁体   English

如何删除记录以及ASP.NET(C#)中的所有相关记录

[英]How to delete a record along with all related records in ASP.NET (C#)

I have this code to delete a photographer from my table, however, photographer_id is a foreign key from my table 'images', and when I delete a photographer I want to delete all the images in the 'images' table by the photographer I am deleting. 我有这段代码可以从表中删除摄影师,但是,photograph_id是表“ images”中的外键,当我删除摄影师时,我想删除摄影师在“ images”表中的所有图像删除。 How do I do that? 我怎么做?

...

else if (e.CommandName == "Slet")
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString =
        ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;

    cmd.CommandText = "DELETE FROM photographers WHERE photographer_id = @photographer_id";

    cmd.Parameters.Add("@photographer_id", SqlDbType.Int).Value = e.CommandArgument.ToString();

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

    Repeater1.DataBind();
}

this is my IMAGES table : 这是我的IMAGES表:

CREATE TABLE [dbo].[images] (
    [image_id] INT  IDENTITY (1, 1) NOT NULL,
    [image] NVARCHAR (50) NOT NULL,
    [FK_photographer] INT NOT NULL,
    PRIMARY KEY CLUSTERED ([billede_id] ASC),
    CONSTRAINT [FK_fotograf] FOREIGN KEY ([FK_fotograf]) REFERENCES [dbo].[Fotografer] ([fotograf_id]),

);

and this is my PHOTOGRAPHERS table : 这是我的PHOTOGRAPHERS表:

CREATE TABLE [dbo].[photographers] (
    [photographer_id] INT IDENTITY (1, 1) NOT NULL,
    [photographer_name] NVARCHAR (50) NOT NULL,
    PRIMARY KEY CLUSTERED ([photographer_id] ASC)
);

Set cascading delete on your foreign key constraint. 在外键约束上设置级联删除 This will automatically delete the Image s when you delete a Photographer`. s when you delete a摄影师s when you delete a这将自动删除Image。

There is no need to reinvent the wheel by doing this yourself. 无需自己动手改造轮子。

Same approach - 相同的方法-

DELETE FROM images WHERE photographer_id = @photographer_id

It is recommended to delete images first and then delete the photographer. 建议先删除图像,然后再删除摄影师。 If your tables are having physical FKs then it won't allow you to delete photographer before you delete all the dependancies. 如果您的表具有物理FK,则将不允许您在删除所有依赖项之前删除摄影师。

Hope this helps. 希望这可以帮助。

As I mentioned in the comment, you can use Cascading Delete . 正如我在评论中提到的,您可以使用级联删除 You can alter your table similar to below. 您可以像下面一样更改表格。

ALTER TABLE billeder
ADD CONSTRAINT fk_photographer
FOREIGN KEY (photographer_id)
REFERENCES photographers (photographer_id)
ON DELETE CASCADE;

Better way is You can do it in Procedures But Here is Substitute and simple way 更好的方法是您可以在“过程”中进行操作,但这是替代和简单的方法

SqlCommand cmd = new SqlCommand();
                        cmd.Connection = conn;
    conn.Open();
    cmd.CommandText = "DELETE FROM photographers WHERE photographer_id = @photographer_id";


cmd.Parameters.Add("@photographer_id", SqlDbType.Int).Value = e.CommandArgument.ToString();


cmd.ExecuteNonQuery();
    //deleted from photographers 

cmd.CommandText = "DELETE FROM Images WHERE photographer_id = @photographer_id";


cmd.Parameters.Add("@photographer_id", SqlDbType.Int).Value = e.CommandArgument.ToString();
     cmd.ExecuteNonQuery();
    //deleted from images 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.net C#SQL。 如果表中存在记录组合,请删除记录 - ASP.net c# SQL. If record combination exists in table, delete the records 在表中保存记录后,如何循环遍历具有相同 ID 的所有记录并在 ASP.NET MVC C# 中更新它们 - After saving a record in a table, how to loop through all records with same ID and update them in ASP.NET MVC C# 如何在 ASP.Net 核心中跟踪相关记录 ID 以更新子记录 - How do I keep track of related record ID's in ASP.Net core for updating child records ASP.net MVC / EF / C#不添加任何相关表记录以在控制器中查询 - ASP.net MVC/EF/C# add none related table records to query in controller 如何在Datatable ASP.NET C#中查找和删除DataRow? - How to Find and delete DataRow in Datatable asp.net c#? 如何在ASP.NET C#中删除特定的listview项目? - How to delete specific listview items in ASP.NET C#? 如何使用 DataSet 和 DataAdapter C#/asp.net 进行删除? - How to delete using DataSet and DataAdapter C#/asp.net? Html,C#,ASP.NET如何重复包含标签的div标签中的内容来显示数据库中的所有记录? - Html, C#, ASP.NET How can i repeat the contents in div tag containing labels to display all records from database? 如何在ASP.NET C#中实现所有数据查看器的此功能? - How to implement this function of all dataviewers in ASP.NET C#? 搜索在SQL表中的字符,并显示在GridView控件与该字符ASP.NET C#开头的所有记录 - Searching a character in sql table and displaying all records in gridview that start with that character ASP.NET c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM