DataTableReader.GetValue(Int32) 方法
定义
获取指定列以本机格式表示的值。Gets the value of the specified column in its native format.
public:
override System::Object ^ GetValue(int ordinal);
public override object GetValue (int ordinal);
override this.GetValue : int -> obj
Public Overrides Function GetValue (ordinal As Integer) As Object
参数
- ordinal
- Int32
从零开始的列序号The zero-based column ordinal
返回
指定列的值。The value of the specified column. 此方法为 null 列返回 DBNull。This method returns DBNull for null columns.
例外
传递的索引超出了 0 到 FieldCount -1 的范围。The index passed was outside the range of 0 to FieldCount - 1.
尝试从已删除的行中检索数据。An attempt was made to retrieve data from a deleted row.
尝试读取或访问关闭的 DataTableReader 中的列。An attempt was made to read or access columns in a closed DataTableReader .
示例
下面的示例循环访问中的当前行内的所有列 DataTableReader ,并显示每个列的内容和列名称。The following example iterates through all the columns within the current row in a DataTableReader, displaying the contents of each column and the column name. 通常,如果您的目的是使用检索的行中的所有列 DataTableReader ,请考虑 GetValues 改用方法,因为它更有效。Generally, if your intent is to work with all the columns within a row retrieved by a DataTableReader, consider using the GetValues method instead, because it is more efficient.
private static void GetAllValues(DataTableReader reader)
{
// Given a DataTableReader, retrieve the value of
// each column, and display the name, value, and type.
// Make sure you have called reader.Read at least once before
// calling this procedure.
// Loop through all the columns.
object value = null;
for (int i = 0; i < reader.FieldCount; i++)
{
if (reader.IsDBNull(i))
{
value = "<NULL>";
}
else
{
value = reader.GetValue(i);
}
Console.WriteLine("{0}: {1} ({2})", reader.GetName(i),
value, reader.GetFieldType(i).Name);
}
}
Private Sub GetAllValues(ByVal reader As DataTableReader)
' Given a DataTableReader, retrieve the value of
' each column, and display the name, value, and type.
' Make sure you've called reader.Read at least once before
' calling this procedure.
' Loop through all the columns.
Dim value As Object
For i As Integer = 0 To reader.FieldCount - 1
If reader.IsDBNull(i) Then
value = "<NULL>"
Else
value = reader.GetValue(i)
End If
Console.WriteLine("{0}: {1} ({2})", reader.GetName(i), _
value, reader.GetFieldType(i).Name)
Next
End Sub
注解
尽管可以在 IsDBNull 调用此方法之前调用来查看是否存在 null 值,但你不必执行此操作。Although you can call IsDBNull to see if there are null values before calling this method, you do not have to do this.