Determines whether the DataReader contains a field. The datareader Field
The GetName () method of DataReader can be used to obtain the field name.
For (int I = 0; I <dataReader. FieldCount; I ++)
{
// Determine whether the fieldName field exists
If (dataReader [I]. GetName (). Equals (fieldName );
}
In the result set received by SqlDataReader in. net, how can I determine whether a field exists?
Since the select name, age from Users, there must be no sex field in sdr.
If it is necessary to judge, write a try catch.
SqlDataReader method for determining whether a Column exists
SqlDataReader dr = Get_dr (xxxx); string ID = dr ["ID"]; // if this column is not found in SQL, an error is returned. I personally think it is best to make a judgment when reading. static bool readerExists (SqlDataReader dr, string columnName) {dr. getSchemaTable (). defaultView. rowFilter = "ColumnName = '" + columnName + "'"; return (dr. getSchemaTable (). defaultView. count> 0);} if (readerExists (dr, "ID") string ID = dr ["ID"]; // if this column is not found in SQL, no error is returned. -------------------------------------------------- the first time I wrote a notebook in the blog Park, I was a newbie. for this reason, you should ignore the technical content you just wrote.