Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlDataReader GetOrdinal with same name

is need to use GetOrdinal from the SqlDataReader but my query is with joins and contains the same field name multiple times. So I try

SELECT a.Id, b.Id FROM table1 AS a ...

but GetOrdinal seems dont understand the schema alias...GetOrdinal('a.Id')` throws an Exception... is there anyway to archive this?

like image 702
Sebastian Avatar asked Sep 02 '14 14:09

Sebastian


People also ask

What is GetOrdinal?

GetOrdinal performs a case-sensitive lookup first. If it fails, a second, case-insensitive search occurs (a case-insensitive comparison is done using the database collation).

How do I check if a DataReader has a column?

string ColumnValue; if (dr["ColumnName"] != null) ColumnValue = dr["ColumnName"].

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

ExecuteReader to retrieve rows from a data source. The DataReader provides an unbuffered stream of data that allows procedural logic to efficiently process results from a data source sequentially. The DataReader is a good choice when you're retrieving large amounts of data because the data is not cached in memory.


1 Answers

Give an alias name in your query

SELECT a.Id As EmployeeID, b.Id as ManagerId FROM table1 AS a ..

Now you can use the Alias names in your code to read the value

var employeeIdIndex = reader.GetOrdinal("EmployeeID")
like image 135
Shyju Avatar answered Oct 13 '22 08:10

Shyju