DataTableReader.Item[] Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the value of the specified column in its native format.
Item[Int32] |
Gets the value of the specified column in its native format given the column ordinal. |
Item[String] |
Gets the value of the specified column in its native format given the column name. |
- Source:
- DataTableReader.cs
- Source:
- DataTableReader.cs
- Source:
- DataTableReader.cs
Gets the value of the specified column in its native format given the column ordinal.
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
Parameters
- ordinal
- Int32
The zero-based column ordinal.
Property Value
The value of the specified column in its native format.
Exceptions
The index passed was outside the range of 0 to FieldCount - 1.
Examples
The following example displays the contents of all the columns, in all the rows from the supplied DataTableReader. The code uses the Item[] method (the indexer, in Microsoft C#) to retrieve the value that is contained in each column.
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
Remarks
This overload for Item[] behaves identically to the GetValue method.
See also
Applies to
.NET 9 ja muud versioonid
Toode | Versioonid |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Source:
- DataTableReader.cs
- Source:
- DataTableReader.cs
- Source:
- DataTableReader.cs
Gets the value of the specified column in its native format given the column name.
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
Parameters
- name
- String
The name of the column.
Property Value
The value of the specified column in its native format.
Exceptions
The name specified is not a valid column name.
An attempt was made to retrieve data from a deleted row.
An attempt was made to read or access a column in a closed DataTableReader.
Examples
Given a DataTableReader and a column name, the GetValueByName procedure returns the value of the specified column. Before calling this procedure, you must create a new DataTableReader instance and call its Read method at least one time to position the row pointer on a row of data.
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
Remarks
A case-sensitive lookup is performed first. If it fails, a second case-insensitive search is made.
This method is kana-width insensitive.
This overloaded version of Item[] corresponds to calling the GetOrdinal method, and then subsequently calling the GetValue method.
Applies to
.NET 9 ja muud versioonid
Toode | Versioonid |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
Toote „.NET“ tagasiside
.NET on avatud lähtekoodiga projekt. Tagasiside andmiseks valige link: