假设我正在调用一个查询“SELECT name,city,country FROM People”。一旦我执行了我的SqlDataReader,列的排列顺序与我的sql查询中的顺序相同吗?SqlDataReader列常规
换句话说,我可以依靠下面的代码将始终正常工作:
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();
}
也多少表现我输,如果我使用的列名,而不是序数(读者[“名称”])?
是否有任何官方的微软文档描述SqlDataReader中列排序的行为?
嗨乔希。感谢您的及时答复。那么这种行为是否有保证,我应该依赖它还是使用列名更好? PS - 至于GetOrdinal,我不认为它对我有用,因为我只检索一行。 – niaher 2009-05-28 04:56:26