简体   繁体   中英

Delete row from 2 related tables

I have 2 tables in my database (Echipa, and Staff) that are connection with an relationship. 在此输入图像描述

To add a row in my tables I just add in the first one, then in the second

private void AddElement(string nume, string an, string tara, string antrenor, string presedinte, string actionar)
{
    conexiune.Open();
    Comanda comanda = new Comanda("insert into echipa values( @nume, @an,@tara)", conexiune);
    comanda.Parameters.Add(new SqlParameter("@nume", nume));
    comanda.Parameters.Add(new SqlParameter("@an", an));
    comanda.Parameters.Add(new SqlParameter("@tara", tara));
    comanda.ExecuteNonQuery();


    comanda = new Comanda("insert into staff values( @antrenor,@presedinte,@actionar)", conexiune);
    comanda.Parameters.Add(new SqlParameter("@antrenor", antrenor));
    comanda.Parameters.Add(new SqlParameter("@presedinte", presedinte));
    comanda.Parameters.Add(new SqlParameter("@actionar", actionar));
    comanda.ExecuteNonQuery();

    conexiune.Close();
    MessageBox.Show("Succes");
}

But what if I want to delete a row for update a row from both? How should I do?

I don't know why but I can't even delete from 1 table

private void DeleteElement(string nume)
{
    conexiune.Open();
    Comanda comanda = new Comanda("delete from echipa where 'nume'=@nume", conexiune);
    comanda.Parameters.Add(new SqlParameter("@nume", nume));

    comanda.ExecuteNonQuery();

    conexiune.Close();
    MessageBox.Show("Succes");
}

This won't do nothing to my tables..

Best way to do that is to setup cascading deletes on the foreign key between the two tables. this way when you delete rows from the master table, all child rows from the second table will be deleted automatically.

Try this. Define the columns after "echipa".

conexiune.Open();
    Comanda comanda = new Comanda("insert into echipa(nume, an, tara) values( @nume, @an,@tara)", conexiune);
    comanda.Parameters.Add(new SqlParameter("@nume", nume));
    comanda.Parameters.Add(new SqlParameter("@an", an));
    comanda.Parameters.Add(new SqlParameter("@tara", tara));
    comanda.ExecuteNonQuery();

I found the solution.

I got 2 tables Echipa - that has EID (int) as primary key and autonumber Staff - that has SID (int) is just autonumber

First i have to delete the row from staff and then from Echipa and it works ! Somewhere up i got using Comanda=System.Data.SqlClient.SqlCommand , is easier for me) and is in my language)

 private void StergeElement(string nume)
    {
        conexiune.Open();


        Comanda comanda = new Comanda("delete from staff where SID=@ID", conexiune);
        comanda.Parameters.Add(new SqlParameter("@ID", nume));

        comanda.ExecuteNonQuery();

         comanda = new Comanda("delete from echipa where EID=@ID", conexiune);
        comanda.Parameters.Add(new SqlParameter("@ID", nume));

        comanda.ExecuteNonQuery();



        conexiune.Close();
        MessageBox.Show("Succes");
    }

Thanks to @apomene for the answer , it helped me a little!

If no deletes are been made,( I guess your field name is nume ) you just have to remove quotes:

Comanda comanda = new Comanda("delete from echipa where nume=@nume", conexiune);
        comanda.Parameters.Add(new SqlParameter("@nume", nume));

        comanda.ExecuteNonQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM