2

I have a datatable in which one of the columns has string values. I want to enforce an unique constraint on that column, but the constraint fails if I have two values that differ only by a space at the end (like "test" and "test "). I don't want this, I want the two values to be considered unique even if the only difference is a space at the end. How can I do this?

try
            {
                DataTable dt = new DataTable("test");
                dt.Columns.Add("nr");
                dt.Columns.Add("text");
                DataRow dr = dt.NewRow();
                dr.ItemArray = new object[] { 1, "test" };
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr.ItemArray = new object[] { 2, "test " };
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr.ItemArray = new object[] { 3, "alabala" };
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr.ItemArray = new object[] { 4, "bbb" };
                dt.Rows.Add(dr);

                DataSet ds = new DataSet("testds");
                ds.Tables.Add(dt);

                ds.EnforceConstraints = true;
                UniqueConstraint unqUID = new UniqueConstraint(ds.Tables[0].Columns[1], true);
                ds.Tables[0].Constraints.Add(unqUID);

                int count = ds.Tables[0].Rows.Count;
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
4

1 回答 1

1

恐怕我不确定你能做些什么。

它似乎只是减少了字符串末尾的空间。如果您在第二行的开头放置一个空格,如下所示:-“测试”。它包括空格并将两行确定为唯一。

我很想知道是否存在解决方案,但我找不到。

于 2012-11-21T10:31:11.157 回答