using System.Data.SqlClient; using System.Data.Common; string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand("SELECT * FROM Customers", connection); connection.Open(); DbDataReader reader = command.ExecuteReader(); Console.WriteLine(reader.GetName(0)); // output: "CustomerID"
using System.Data.SqlClient; using System.Data.Common; string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand("SELECT * FROM Customers", connection); connection.Open(); DbDataReader reader = command.ExecuteReader(); for(int i = 0; i < reader.FieldCount; i++) { Console.WriteLine(reader.GetName(i)); }In this example, we create a loop to iterate over all the columns in the result set and use the GetName method to get the name of each column. This is useful when we want to programmatically manipulate the data based on column names. The package library for this class is the System.Data.Common namespace, which is included in the .NET framework.