在前面的课程中,我们使用SqlDataReader的索引器,比如rdr[0],提取行中的第一列。你能够使用诸如这样的数值索引器提取行中的列,但是它并不具有很好的可读性。上面的例子使用了字符串索引器,这里的字符串是从SQL查询语句中得到的列名(表的列名如果你使用一个星号,*.字符串下标具有更好的可读性,使得代码能够更好的维护。
Regardless of the type of the indexer parameter, a SqlDataReader indexer will return type object. This is why the example above casts results to a string. Once the values are extracted, you can do whatever you want with them, such as printing them to output with Console type methods.
无论索引器参数是什么类型,一个SqlDataReader索引器将返回object类型。这就是为什么上面要将结果转换为string的原因。只要值被提取,你能够对它们为所欲为,比如使用Console类型的方法将它们打印到输出。
Finishing Up