Try:
Convert.IsDBNull(reader["Col1"]) ? "null" : reader["Col1"]
Alternatively, if you're going to be using this repeatedly, this is probably an ideal candidate for a tightly scoped Extension Method, for example:
public static class ExtensionMethods
{
public static object GetValueOrNull(this SqlDataReader reader, string key)
{
return Convert.IsDBNull(reader[key]) ? (object)"null" : reader[key];
}
}
So you could then write:
var valueOfReader = reader.GetValueOrNull("Col1");
Which would definately make things tidier if you needed to use this logic multiple times inside one StringBuilder.AppendFormat call:
while (reader.Read())
{
sb.AppendFormat("{0},{1},{2},",reader.GetValueOrNull("Col1"), reader.GetValueOrNull("Col2"), reader.GetvalueOrNull("Col3"));
}