假設我正在調用一個查詢「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