只插入新数据,而不是复制现有数据

本文关键字:数据 复制 新数据 插入 | 更新日期: 2023-09-27 18:06:44

我有这段代码在运行,从核心系统调用数据,

  public int MuatTurunMTS(hopesWcfRef.Manifest2 entParam, string destination, int capacity)
        {
        try
        {
            EL.iSeriesWcf.IiSeriesClient iClient = new iSeriesWcf.IiSeriesClient();
            EL.iSeriesWcf.Manifest2 manifest2 = new iSeriesWcf.Manifest2();
            manifest2 = iClient.GetManifest2(entParam.NOKT, destination, capacity);
            DataTable dt = new DataTable();
            dt = manifest2.Manifest2Table;
            List<hopesWcfRef.Manifest2> LstManifest2 = new List<hopesWcfRef.Manifest2>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                hopesWcfRef.Manifest2 newDataRow = new hopesWcfRef.Manifest2();
                newDataRow.NOPASPOT = dt.Rows[i][1].ToString();
                newDataRow.NOKT = dt.Rows[i][0].ToString();
                newDataRow.KOD_PGKT = dt.Rows[i][2].ToString();
                newDataRow.KOD_KLGA = dt.Rows[i][3].ToString();
                newDataRow.NAMA = dt.Rows[i][4].ToString();
                newDataRow.TKHLAHIR = (dt.Rows[i][5].ToString() == "1/1/0001 12:00:00 AM" ? DateTime.Now : Convert.ToDateTime(dt.Rows[i][5]));  //Convert.ToDateTime(dt.Rows[i][5]);
                newDataRow.JANTINA = dt.Rows[i][6].ToString();
                newDataRow.WARGANEGARA = dt.Rows[i][7].ToString();
                newDataRow.JNS_JEMAAH = dt.Rows[i][8].ToString();
                newDataRow.NO_SIRI = dt.Rows[i][9].ToString();
                newDataRow.NO_MS = Convert.ToInt16(dt.Rows[i][10]);
                newDataRow.BARKOD = dt.Rows[i][13].ToString();
                newDataRow.NO_DAFTAR = dt.Rows[i][14].ToString(); 
                //bydefault make cell empty
                newDataRow.STS_JEMAAH = "";
                newDataRow.SEAT_NO = "";
                newDataRow.SEAT_ZONE = "";
                newDataRow.BERAT = 0;
                newDataRow.JUM_BEG = 0;
                LstManifest2.Add(newDataRow);
            }
            int cntr = 0;
            if (LstManifest2.Count != 0)
            {
                foreach (hopesWcfRef.Manifest2 manifest in LstManifest2)
                {
                    cntr++;
                    SaveManifestTS(manifest);
                }
            }
            return LstManifest2.Count;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

和到:

 public void SaveManifestTS(hopesWcfRef.Manifest2 manifest)
         {
             try
             {
                 ent.BeginSaveChanges(SaveChangesOptions.Batch, null, null);
                 ent.AddObject("Manifest2", manifest);
                 ent.SaveChanges(SaveChangesOptions.Batch);
              }
             catch (Exception ex)
             {
                 //Uri u = new Uri(ent.BaseUri + "GetErrorMsg"
                 //              , UriKind.RelativeOrAbsolute);
                 //string datas = (ent.Execute<string>(u)).FirstOrDefault();
                 //Exception ex = new Exception(datas);
                 throw ex;
             }
         }

当SaveChanges运行时,如果数据存在,它将复制整个行,如何避免数据被重复时插入(savechanges)??????非常感谢

只插入新数据,而不是复制现有数据

关于:不插入

在这些情况下,我使用MERGE语句更新现有数据(基于主键)并插入新数据。

哦,你喜欢张贴的所有代码示例都与问题完全无关,这是一个纯粹的SQL方面的问题。