0
我想在數據庫表上運行查詢,並讓它按字母順序返回結果,但是當我運行以下語句時,它將返回所有內容它出現在數據庫中的方式。我正在按照我所瞭解的應該按字母順序返回結果順序的辦公組執行命令。我的SQL語句沒有按字母順序返回我的查詢
SELECT
OfficeGroupID, OfficeGroup
FROM
tblofficegroup
INNER JOIN
tblRegion ON tblOfficeGroup.RegionID = tblRegion.RegionID
WHERE
ISNULL(tblofficegroup.Hide, 0) = 0
AND ISNULL(tblRegion.Hide, 0) = 0
AND OfficeGroupID <> 18 --'Not Mapped'
ORDER BY
OfficeGroup -- this should return it in alphabetical order but nogame.
-- I also tried passing the ASC command but neither worked
這是從數據庫中的表,我顯示在網頁上查詢,它體現在它是在表上出現的順序相同。我用ASC
,但沒有運氣,這是一個字符串類型(varchar(32)
)。
這是使查詢我的網頁的功能,我看不出什麼錯:
public static List<Market> GetMarketGroup()
{
List<Market> regionList = new List<Market>();
using (SqlConnection connection = new SqlConnection(_constring))
{
StringBuilder sqlCommandBuilderTxt = new StringBuilder();
sqlCommandBuilderTxt.Append("SELECT OfficeGroupID,OfficeGroup FROM tblofficegroup INNER JOIN tblRegion ON tblOfficeGroup.RegionID = tblRegion.RegionID WHERE ISNULL(tblofficegroup.Hide, 0) = 0 AND ISNULL(tblRegion.Hide, 0) = 0 AND OfficeGroupID <> 18 --'Not Mapped' ORDER BY OfficeGroup ASC ");
SqlCommand sqlCommand = new SqlCommand(sqlCommandBuilderTxt.ToString(), connection);
try
{
connection.Open();
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
try
{
Market market = new Market();// instance
market.MarketID = reader.GetInt32(0);
market.MarketGroup = reader.GetString(1);
regionList.Add(market);
}
catch (Exception ex)
{
log.Error(ex);
}
}
reader.Close();
connection.Close();
}
catch (Exception ex)
{
log.Error(ex);
}
}
return regionList;
}
您可以顯示結果如何? – Lamak
什麼是實際數據?結果是什麼?該欄的類型是什麼?實際上,「ORDER BY」確實排序了數據。我懷疑你以某種方式驗證了它的錯誤。 – David
以及您在查看結果的內部 –