| 在前面的课程中,我们使用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
完结
Always remember to close your SqlDataReader, just like you need to close the SqlConnection. Wrap the data access code in a try block and put the close operation in the finally block, like this:
一定要记住关闭SqlDataReader,就像关闭SqlConnection一样。将数据存取代码用try语句块包围起来,并把关闭操作放到finally语句块中,就像这样:
try
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ... 下一页 >> |