【问题标题】:compare two IEnumerable and delete/insert based on the comparison result根据比较结果比较两个 IEnumerable 和 delete/insert
【发布时间】:2013-01-23 11:28:06
【问题描述】:

我必须比较 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.
             }
         }

让IDistributionListType的值来自UI:

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

让IDistributionListType的值来自DB:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多