METHOD

DbDataReader.GetFieldValue<T>

Reads a column from the current row as the specified type. Check IsDBNull first for nullable fields. Supported conversions depend on the concrete reader; this is not a universal conversion utility.

Formal Syntax & Structure

csharp syntaxcsharp
reader.GetFieldValue<int>(ordinal)

Working Example

Example Snippetcsharp
using System;using System.Data;using System.Data.Common;using var t=new DataTable(); t.Columns.Add("Id",typeof(int)); t.Rows.Add(7); using var r=t.CreateDataReader(); if(r.Read()) Console.WriteLine(r.GetFieldValue<int>(0)); // 7
C# & .NET Reference