且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止将重复记录插入到数据表中

更新时间:2023-01-29 18:56:57

您可以使用唯一约束 [ ^ ]来完成这项工作。
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..