Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# Check if SQLDataReader has field or field is null
// assumes dr is a SqlDataReader
List<string> columns_ = GetColumns(dr);
if (columns_.IndexOf("MissingField") == -1) { throw new Exception("Query did not contain \"MissingField\""); }
this.DBID = (int)dr["ID"];
this.Created = (DateTime)dr["Created"];
this.DataText = (string)dr["MissingField"];
if (!dr.IsDBNull(columns_.IndexOf("MsgType")) { this.MsgType = (string)dr["MsgType"]; }
if (!dr.IsDBNull(columns_.IndexOf("MsgDriver")) { this.MsgDriver = (int)dr["MsgDriver"]; }
if (!dr.IsDBNull(columns_.IndexOf("MsgJob")) { this.MsgDriver = (int)dr["MsgJob"]; }
}
public List<string> AllColumnNames(SqlDataReader dr)
{
List<string> res = new List<string>();
for (int i = 0; i < dr.FieldCount; i++)
{
res.Add(dr.GetName(i));
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.