using System; using System.Data; using System.Data.SqlClient; namespace GetOrdinal { class ClassTest { static void Main(string[] args) { SqlConnection conn = null; SqlCommand command = null; SqlDataReader dr = null; try { conn = new SqlConnection("XXXXXXXXXXXXXXXXXXXXXXX"); conn.Open(); command = new SqlCommand("SELECT Nom, Idclient from Client",conn); dr = command.ExecuteReader(); while(dr.Read()) Console.WriteLine(dr["Nom"]); } catch(Exception ex) { Console.WriteLine(ex.Message); } finally { dr.Close(); command.Dispose(); conn.Close(); } } } }
int ordColumnName = dr.GetOrdinal("Nom"); while(dr.Read()) Console.WriteLine(dr[ordColumnName]);