2013-01-23 165 views
0

我必须比较来自UI和来自SQL Server 2008的IEnumerable数据的IEnumerable数据,并且必须从UI插入/更新新元素到数据库并从元素中删除元素基于UI数据的数据库。比较两个IEnumerable并根据比较结果删除/插入

我有在DB Distribution_X_ListType表:

DistributionID ListTypeID EmployeeNumber DepartmentID LocationID 
1    2   84528   NULL   NULL 
1    3   NULL   8051   NULL 
1    5   NULL   NULL   319 

我有一个项目的界面如下:

public interface IDistributionList : IEntity 
    { 
     string Name { get; set; } 
     bool ActiveFlag { get; set; } 
     ........................... 
     ........................... 
     IEnumerable<IDistributionListType> ListTypes { get; set; }   
    } 

的另一个接口是:

public interface IDistributionListType 
{ 
    int? DistributionID { get; set; } 
    int ListTypeID { get; set; } 
    int EmployeeNumber { get; set; } 
    int DepartmentID { get; set; } 
    int LocationID { get; set; } 
} 

在另一个项目中,我具有Save功能,如下所示:

public int Save(IDistributionList distributionList) 
     { 
      SqlDataReader reader = null;    
      int rowsaffected = 0; 
      try 
      { 
       sqlcommand = new SqlCommand("spGetDistributionListTypeByID", con); //spGetDistributionListTypeByID - This SP returns all the members from Distribution_X_ListType table for the given distribution ID 
       sqlcommand.CommandType = CommandType.StoredProcedure; 
       sqlcommand.Parameters.AddWithValue("@Distribution_ID", distributionList.ID); 

       reader = sqlcommand.ExecuteReader(); 

       while (reader.Read()) 
       { 
         ????Value Should be stored in an IDistributionListType variable,say IDistributionListTypeFromDB 
       } 
       ????IDistributionListType from function parameter(Lets say from UI) should be compared with the above IDistributionListTypeFromDB data. 
       ????Insert/Delete should be done in DB by comparing the data. 
      } 
     } 

从UI让IDistributionListType的价值:

DistributionID ListTypeID EmployeeNumber DepartmentID LocationID 
1    2   84528   NULL   NULL 
1    5   NULL   NULL   64 

从DB让IDistributionListType的价值:

DistributionID ListTypeID EmployeeNumber DepartmentID LocationID 
1    2   84528   NULL   NULL 
1    3   NULL   8051   NULL 
1    5   NULL   NULL   319 

我需要从UI的数据来更新数据库。我有两个的SP为:

spInsertUpdateDistributionListType - Insert/Update Distribution_X_ListType table based on DistributionID and listtypeID 
spDeleteDistributionListType - Delete Distribution_X_ListType table based on DistributionID and listtypeID 

我不知道如何在代码????区域(在.net代码中提到)。有人请帮忙。提前致谢。

回答

0

你应该只使用一个O/R映射器,如Entity Framework,它会为你处理。