根据比较结果比较两个IEnumerable和delete / insert

时间:2013-01-23 11:28:07

标签: c# .net sql-server sql-server-2008 ienumerable

我必须比较来自UI的IEnumerable数据和来自SQL Server 2008的IEnumerable数据,并且必须将新元素从UI插入/更新到DB,并根据UI数据从DB中删除元素。

我在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; }
}

在另一个项目中,我有如下保存功能:

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代码中提到)。有人请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

你应该使用像Entity Framework这样的O / R映射器,它会为你处理这个问题。