avoid checking for DataRow.IsDBNull on each column?

前端 未结 4 1841
无人及你
无人及你 2021-01-19 07:46

My code is 2x longer than it would be if I could automatically set IsDBNull to \"\" or simply roll over it without an error.

This is my cod

4条回答
  •  没有蜡笔的小新
    2021-01-19 08:04

    Here's a one-liner:

    Response.Write(rs.IsNull("column") ? "" : rs("column"));
    

    or make it an extension method:

    public string GetValueOrBlankString(this DataRow rs, string column)
    {
        return rs.IsNull(column) ? "" : rs(column).ToString();
    }
    

    then call it as:

    Response.Write(rs.GetValueOrBlankString("column"));
    

提交回复
热议问题