Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the datatype of Field from DataReader object

Tags:

c#

asp.net

I have following query:

SqlCommand cmd = new SqlCommand("Select employee_id,            lastname, firstname from Employees", conn);  // Execute reader SqlDataReader reader = cmd.ExecuteReader(); 

Suppose I want to know the datatype of field employee_id. How do I determine this using the SqlDataReader?

like image 341
ghanshyam.mirani Avatar asked Jul 29 '13 09:07

ghanshyam.mirani


People also ask

How to get values from DataReader in c#?

To retrieve data using a DataReader, create an instance of the Command object, and then create a DataReader by calling Command. ExecuteReader to retrieve rows from a data source.

Which function of DataReader is used to get the data from it?

Use the DataReader. Read method to obtain a row from the query results. You can access each column of the returned row by passing the name or ordinal number of the column to the DataReader.

What is DataReader object?

In ADO.NET, a DataReader is a broad category of objects used to sequentially read data from a data source. DataReaders provide a very efficient way to access data, and can be thought of as a Firehose cursor from ASP Classic, except that no server-side cursor is used.


1 Answers

reader.GetFieldType(int ordinal)

will return the .NET type of the field, while:

reader.GetDataTypeName(int ordinal)

will return a string representing the data type of the field in the data source (e.g. varchar). GetFieldType is likely to be more useful to you given the use case you describe

like image 101
Jon G Avatar answered Sep 21 '22 08:09

Jon G