sql - How to delete a record along with all related records in ASP.NET (C#) -



sql - How to delete a record along with all related records in ASP.NET (C#) -

i have code delete photographer table, however, photographer_id foreign key table 'images', , when delete photographer want delete images in 'images' table photographer deleting. how 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 photographers photographer_id = @photographer_id"; cmd.parameters.add("@photographer_id", sqldbtype.int).value = e.commandargument.tostring(); conn.open(); cmd.executenonquery(); conn.close(); repeater1.databind(); }

this images table :

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 photographers table :

create table [dbo].[photographers] ( [photographer_id] int identity (1, 1) not null, [photographer_name] nvarchar (50) not null, primary key clustered ([photographer_id] asc) );

as mentioned in comment, can utilize cascading delete. can alter table similar below.

class="lang-sql prettyprint-override">alter table billeder add together constraint fk_photographer foreign key (photographer_id) references photographers (photographer_id) on delete cascade;

c# sql asp.net

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -