【问题标题】:Delete row from 2 related tables从 2 个相关表中删除行
【发布时间】:2014-05-08 14:45:14
【问题描述】:

我的数据库中有 2 个表(Echipa 和 Staff)与关系相关联。

要在我的表格中添加一行,我只需在第一个中添加,然后在第二个中添加

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");
}

但是如果我想删除一行来更新一行呢? 我该怎么办?

我不知道为什么,但我什至无法从 1 个表中删除

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");
}

这不会对我的桌子造成任何影响..

【问题讨论】:

    标签: c# sql sql-server database


    【解决方案1】:

    最好的方法是在两个表之间的外键上设置级联删除。这样当你从主表中删除行时,第二个表中的所有子行都会被自动删除。

    【讨论】:

    • 这也是我处理它的方式。或者,OP 可以发出两次删除,但确保它们在父级之前删除子级,最好是在事务中。
    • 你能提供一些代码或链接到一些例子吗?谢谢!
    【解决方案2】:

    试试这个。定义“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();
    

    【讨论】:

    • 我想从两个表中删除一行,我展示了如何将元素添加到表中并且有效,我不知道如何删除或更新它们...如果我从一个中删除,则会收到错误 DELETE 语句与 REFERENCE 约束“FK_Staff_Echipa”冲突。冲突发生在数据库“..
    【解决方案3】:

    我找到了解决办法。

    我有 2 张桌子 Echipa - 以 EID (int) 作为主键和自动编号 员工 - 具有 SID (int) 的只是自动编号

    首先我必须从员工中删除该行,然后从 Echipa 中删除,它可以工作! 我在某个地方使用 Comanda=System.Data.SqlClient.SqlCommand ,对我来说更容易)并且是我的语言)

     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");
        }
    

    感谢@apomene 的回答,对我有点帮助!

    【讨论】:

      【解决方案4】:

      如果没有删除,(我猜你的字段名称是nume)你只需要删除引号:

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

      【讨论】:

      • 是的,我错了,但我无法删除它,因为它连接到第二个数据库,我收到错误 DELETE 语句与 REFERENCE 约束“FK_Staff_Echipa”冲突。数据库中发生冲突“
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多