如何防止将重复记录插入到数据表中 [英] How to prevent insertions of duplicate records into datatable

查看:71
本文介绍了如何防止将重复记录插入到数据表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach (Selected s1 in objlistCropped)
                   {
                       string strImg2 = s1.Image;

                       if (strImg1 == strImg2)
                       {
                           dtPreInstallation.Rows.Add(strImg2, s.strText);
                       }
                    }







strImg1 = Img1.Jpeg,
我需要限制再添加img1.jpeg,如果它已经存在于数据表中。



先谢谢




strImg1 = Img1.Jpeg,
I need to restrict adding img1.jpeg again if its already there in datatable.

Thanks in Advance

推荐答案

您可以使用唯一约束 [ ^ ]来完成这项工作。
You can use a Unique constraint[^] to do the job.


foreach (Selected s1 in objlistCropped)
            {
                //here you check the value is already added in the DataTable or not.
                bool duplicate = false;
                foreach (DataRow dr in dtPreInstallation.Rows)
                {
                    if (dr["columnName"].ToString() == "value") //compare the new value with the values in the datatable
                    {
                        duplicate = true;
                        return;
                    }
                }

                if (duplicate != true)
                {
                    dtPreInstallation.Rows.Add(strImg2, s.strText);
                }
                else
                {
                    //message : Value already exists
                }
            }







试试这个..




try this..


这篇关于如何防止将重复记录插入到数据表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆