Determines whether the DataReader contains a field. The datareader Field

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.