[英]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.