DataTableReader.Item[] 属性

定义

获取指定列的本机格式的值。

重载

名称 说明
Item[Int32]

获取给定列序号的指定列的本机格式的值。

Item[String]

获取给定列名称的本机格式指定列的值。

Item[Int32]

Source:
DataTableReader.cs
Source:
DataTableReader.cs
Source:
DataTableReader.cs
Source:
DataTableReader.cs
Source:
DataTableReader.cs

获取给定列序号的指定列的本机格式的值。

public:
 virtual property System::Object ^ default[int] { System::Object ^ get(int ordinal); };
public override object this[int ordinal] { get; }
member this.Item(int) : obj
Default Public Overrides ReadOnly Property Item(ordinal As Integer) As Object

参数

ordinal
Int32

从零开始的列序号。

属性值

指定列的本机格式的值。

例外

传递的索引超出 0 到 FieldCount 1 的范围。

示例

以下示例显示提供 DataTableReader的所有行中所有列的内容。 该代码使用 Item[] 方法(索引器,Microsoft C# 中)检索每列中包含的值。

private static void DisplayItems(DataTableReader reader)
{
    int rowNumber = 0;
    while (reader.Read())
    {
        Console.WriteLine("Row " + rowNumber);
        for (int i = 0; i < reader.FieldCount; i++)
        {
            Console.WriteLine("{0}: {1}", reader.GetName(i), reader[i]);
        }
        rowNumber++;
    }
}
Private Sub DisplayItems(ByVal reader As DataTableReader)
   Dim rowNumber As Integer
   While reader.Read()
      Console.WriteLine("Row " & rowNumber)
      For i As Integer = 0 To reader.FieldCount - 1
         Console.WriteLine("{0}: {1}", reader.GetName(i), reader.Item(i))
      Next
      rowNumber += 1
   End While
End Sub

注解

此重载 Item[] 的行为与方法相同 GetValue

另请参阅

适用于

Item[String]

Source:
DataTableReader.cs
Source:
DataTableReader.cs
Source:
DataTableReader.cs
Source:
DataTableReader.cs
Source:
DataTableReader.cs

获取给定列名称的本机格式指定列的值。

public:
 virtual property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ name); };
public override object this[string name] { get; }
member this.Item(string) : obj
Default Public Overrides ReadOnly Property Item(name As String) As Object

参数

name
String

列的名称。

属性值

指定列的本机格式的值。

例外

指定的名称不是有效的列名。

尝试从已删除的行中检索数据。

尝试读取或访问关闭 DataTableReader中的列。

示例

给定一个 DataTableReader 列名和一个列名,GetValueByName 过程返回指定列的值。 在调用此过程之前,必须创建新 DataTableReader 实例并调用其 Read 方法,至少一次才能将行指针置于数据行上。

private static object GetValueByName(
    DataTableReader reader, string columnName)
{
    // Consider when to use a procedure like this one carefully:
    // if  you're going to retrieve information from a column
    // in a loop, it would be better to retrieve the column
    // ordinal once, store the value, and use the methods
    // of the DataTableReader class directly.
    // Use this string-based indexer sparingly.
    object columnValue = null;

    try
    {
        columnValue = reader[columnName];
    }
    catch (ArgumentException ex)
    {
        // Throw all other errors back out to the caller.
        columnValue = null;
    }
    return columnValue;
}
Private Function GetValueByName( _
   ByVal reader As DataTableReader, _
   ByVal columnName As String) As Object

   ' Consider when to use a procedure like this one carefully:
   ' If you're going to retrieve information from a column
   ' in a loop, it would be better to retrieve the column
   ' ordinal once, store the value, and use the methods
   ' of the DataTableReader class directly. 
   ' Use Item(columnName) sparingly.
   Dim columnValue As Object

   Try
      columnValue = reader.Item(columnName)
   Catch ex As ArgumentException
      ' Throw all other errors back out to the caller.
      columnValue = Nothing
   End Try
   Return columnValue
End Function

注解

首先执行区分大小写的查找。 如果失败,则会进行第二次不区分大小写的搜索。

此方法不区分假名宽度。

此重载版本的 Item[] 对应调用 GetOrdinal 方法,然后随后调用该方法 GetValue

适用于