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
?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With