0

我在 SQL 中有 2 个表,它们都有一个名为 Selection_ID 的字段。我想Selection_ID = inpSelectionID使用 Linq-to-SQL 删除所有行。

我的桌子: 在此处输入图像描述

我的 C# 函数:

void buttonDeleteSelectionList_Click(object sender, RoutedEventArgs e, Canvas canvasAdvancedSearchFieldResults, int inpSelectionID)
{

    PositionServiceReference.PositionServiceClient service = new PositionServiceReference.PositionServiceClient();
    service.DeleteSelectionCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(service_DeleteSelectionCompleted);
    service.DeleteSelectionAsync(inpSelectionID);
}

我的服务代码 PositionService.svc.cs:

[OperationContract]
void DeleteSelection(int inpSelectionID)
{
    PositionDataClassesDataContext context = new PositionDataClassesDataContext();

    context.Lloyds_Selection.DeleteOnSubmit(inpSelectionID);
    context.SubmitChanges();

    context.Lloyds_Selection_Vessels.DeleteOnSubmit(inpSelectionID);
    context.SubmitChanges();
}
4

1 回答 1

1

DeleteOnSubmit需要实体对象作为参数,所以如果不先从数据库中选择它就不能删除它。也有DeleteAllOnSubmit方法,但也需要IEnumerable实体。你可以这样使用它:

context.Lloyds_Selection.DeleteAllOnSubmit(context.Lloyds_Selection.Where(l => l.Selection_ID  == inpSelectionID));

但是,您可以使用DataContext.ExecuteCommand对数据库执行原始 SQL:

string command = string.format("DELETE FROM Lloyds_Section WHERE Selection_ID = {0}", inpSelectionID");
context.ExecuteCommand(command );
于 2013-03-14T12:08:11.757 回答