DataTable dt = new DataTable();
res.Fill(dt);
获取sql数据代码。
DataRow dr = dt.NewRow();
dr[0] = tt_id;
dr[1] = fault_desc;
dr[2] = tt_time;
dt.Rows.Add(dr);
DataView dv = new DataView(dt);
Response.Write(dv);
if (dv.Count != dv.ToTable(true, "tt_id").Rows.Count) {
// string sqlstr = string.Format("insert
}
我想使用dataview检查以前在sql中存在的新数据 如果存在取消插入。 问题是响应说我有相同的价值 但我检查它在dv中没有任何相同的值。 这里有什么问题?
答案 0 :(得分:1)
var duplicateValues = (from row in dt.AsEnumerable()
orderby row.Field<string>("Id")
select new DuplicateObject
{
Id = row.Field<string>("Id"),
Name = row.Field<string>("Name"),
Skill = row.Field<string>("Skill")
}).Distinct(new DuplicateObjectComparer()).ToList();
string dupValue = string.Empty;
foreach (var dup in duplicateValues)
{
dupValue = dup.Id + " - " + dup.Name + " - " + dup.Skill;
Console.WriteLine("Duplicate entry:" + dupValue);
}
if (duplicateValues.Count == 0)
Console.WriteLine("No duplicate entry");
// Supporting classes
// Gives a strongly type class from the Linq query
public class DuplicateObject
{
public string Id { get; set; }
public string Name { get; set; }
public string Skill { get; set; }
}