「SELECT name, city, country FROM People」というクエリを呼び出しているとします。SqlDataReader を実行すると、SQL クエリと同じ順序で列が表示されますか?
つまり、次のコードが常に正しく機能することを信頼できますか。
SqlConnection connection = new SqlConnection(MyConnectionString);
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT [name], [city], [country] WHERE [id] = @id";
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.SingleRow);
if (reader.Read())
{
// Read values.
name = reader[0].ToString();
city = reader[1].ToString();
country = reader[2].ToString();
}
}
catch (Exception)
{
throw;
}
finally
{
connection.Close();
}
また、序数 (reader["name"]) の代わりに列名を使用すると、どの程度のパフォーマンスが失われますか?
SqlDataReader での列の順序付けの動作を説明している公式の Microsoft ドキュメントはありますか?