开发者

Check Constraints on DataRow Add

开发者 https://www.devze.com 2022-12-17 17:26 出处:网络
DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn(\"ValueOne\",typeof(string)){AllowDBNull = false});
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("ValueOne",typeof(string)){AllowDBNull = false});
    dt.Columns.Add(new DataColumn("ValueTwo",typeof(string)){AllowDBNull = false});

    DataRow row = dt.NewRow();
    row["Value开发者_JS百科One"] = "Test1";
    if (dt.Rows.CanAdd(row))
    {
        dt.Rows.Add(row);
    }

Is there some way to Check if a Row Can be Added before trying to add the row?


There is no way to automatically do this. What would you do instead? If you just want to skip the row you could put it into a try/catch but make sure you only catch the specific exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消