C# DataRow Empty-check

后端 未结 11 1128
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 05:13

I got this:

 DataTable dtEntity = CreateDataTable();
 drEntity = dtEntity.NewRow();

Then I add data to the row (or not). Lots of code, real

11条回答
  •  盖世英雄少女心
    2020-12-15 06:14

    DataTable.NewRow will initialize each field to:

    • the default value for each DataColumn (DataColumn.DefaultValue)

    • except for auto-increment columns (DataColumn.AutoIncrement == true), which will be initialized to the next auto-increment value.

    • and expression columns (DataColumn.Expression.Length > 0) are also a special case; the default value will depend on the default values of columns on which the expression is calculated.

    So you should probably be checking something like:

    bool isDirty = false;
    for (int i=0; i 0) continue;
        if (table.Columns[i].AutoIncrement) continue;
        if (row[i] != table.Columns[i].DefaultValue) isDirty = true;
    }
    

    I'll leave the LINQ version as an exercise :)

提交回复
热议问题