按钮点击代码

bindingSource1.EndEdit();
            try
            {
                // Delete all the Channels for the current Folder.
                int folderID = (int)RemoveFolderBox.SelectedValue;
                deleteChannels(folderID);

                // Delete the folder itself.
                RSSDataSet1.FolderRow folder = rSSDataSet1.Folder.FindByFolderID(folderID);
                folder.Delete();

                int rowsAffected = folderTableAdapter.Update(rSSDataSet1);

                if (rowsAffected > 0)
                {
                    //statusLabel.Text = "Folder successfully deleted.";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem when deleting folder:" + ex.Message);
            }


deleteChannels功能:

private void deleteChannels(int folderID)
{
    try
    {
       RSSDataSet1.ChannelRow[] channels = (RSSDataSet1.ChannelRow[])rSSDataSet1.Channel.Select("FolderID = " + folderID.ToString());

       foreach (RSSDataSet1.ChannelRow channel in channels)
       {
          int channelID = channel.ChannelID;
          channel.Delete();
       }
       int rowsAffected = channelTableAdapter1.Update(rSSDataSet1);
     }
     catch (Exception ex)
     {
       throw ex;
     }
}


我越来越


  DELETE语句与REFERENCE约束冲突
  “ FK_Channel_Folder”。数据库中发生了冲突
  “ C:\ USERS \ ADMIN \ DOCUMENTS \ VISUAL STUDIO 2010 \ PROJECTS \ PROJ RSS \ PROJ
  RSS \ BIN \ DEBUG \ RSS.MDF”,表“ dbo.Channel”的“ FolderID”列。
  声明已终止。

最佳答案

如果您要删除的项目被删除,则您有一个外键引用将被违反。看起来Channel具有依赖于它的Folder实体。

在删除Folder之前,您将需要删除所有相关的Channel实体,或设置级联删除。

08-25 12:59
查看更多