日期:2014-05-18  浏览次数:20428 次

求助:添加数据重复则Update的问题
这是向DB中添加数据, 但是我想在添加之前根据ID和ParentGroups判断, 如果ID没有重复则添加, 而重复了则判断ParentGroups是否和DB中一样, 如果不一样则只update ParentGroups.
但是用下面这个方法我不知道如何修改.请大侠帮帮忙。
C# code

private const string SQL_GROUP_SELECT =
            @"      SELECT  [Id],[name],[description],[groupType],[owner],[coowner],
                            [CreatedDate],[ChangedDate],[IsGSSGroup],
                            [AutoAcceptOUChange],[CompanyCode],[AutoExpiredDate],[ParentGroups]
                    FROM    [Group]";

        private const string DefaultTableName = "Pending_Group";

private static void InitializeTable(string tableName)
        {
            PendingGroupTable.TableName = tableName;

            PendingGroupTable.Columns.Add("ID", typeof(string));
            PendingGroupTable.Columns.Add("Name", typeof(string));
            PendingGroupTable.Columns.Add("Description", typeof(string));
            PendingGroupTable.Columns.Add("GroupType", typeof(int));
            PendingGroupTable.Columns.Add("Owner", typeof(string));
            PendingGroupTable.Columns.Add("Coowner", typeof(string));
            PendingGroupTable.Columns.Add("CreatedDate", typeof(DateTime));
            PendingGroupTable.Columns.Add("ChangedDate", typeof(DateTime));
            PendingGroupTable.Columns.Add("IsGSSGroup", typeof(bool));
            PendingGroupTable.Columns.Add("AutoAcceptOUChange", typeof(bool));
            PendingGroupTable.Columns.Add("CompanyCode", typeof(string));
            PendingGroupTable.Columns.Add("AutoExpiredDate", typeof(DateTime));
            PendingGroupTable.Columns.Add("ParentGroups", typeof(string));
            PendingGroupTable.Columns.Add("ImportStatus", typeof(string));

            PendingGroupTable.PrimaryKey = new[] { PendingGroupTable.Columns["ID"] };
        }


C# code

private static void UpdateToDB()
        {
            try
            {
                var conn = new SqlConnection(ConnString.ConnectionString);

                var daGroup = new SqlDataAdapter(SQL_GROUP_SELECT, conn)
                {
                    MissingSchemaAction = MissingSchemaAction.AddWithKey,
                };
                var scbGroup = new SqlCommandBuilder(daGroup);

                DataTable dtTemp = PendingGroupTable.Clone();

                foreach (DataRow dr in PendingGroupTable.Rows)
                {
                    if (!string.IsNullOrEmpty(dr["ImportStatus"].ToString()))
                    {
                        if (Equals(ImportStatus.Ready, Enum.Parse(typeof(ImportStatus), dr["ImportStatus"].ToString())))
                        {
                            int colCount = dtTemp.Columns.Count;
                            DataRow drNew = dtTemp.NewRow();
                            for (int i = 0; i < colCount - 1; i++)
                            {
                                drNew[i] = dr[i];
                            }
                            dtTemp.Rows.Add(drNew);
                        }
                    }
                }
                dtTemp.Columns.Remove("ImportStatus");

                daGroup.Fill(dtTemp);
                daGroup.Update(dtTemp);

                //PendingGroupTable.AcceptChanges();
                //return true;
            }
            catch (Exception)
            {
                //return false;
            }

            pendingGroupEmployees.Update();
        }



------解决方案--------------------
int Count=select Count(*) from