您能否帮助我使用asp.net为表格中的显示数据提供示例。
我使用此SQL查询:
SELECT
pvt.CityName,
pvt.[Deluxe Class],
pvt.[Superior Class],
pvt.[Standard Class]
FROM
(SELECT
c.CityName,
h.HotelName,
tc.TourClass
FROM
tblCity2 c
LEFT JOIN
tblTourHotel2 th ON c.CityID = th.CityID
LEFT JOIN
tblHotel2 h ON th.HotelID = h.HotelID
LEFT JOIN
tblTourClass2 tc ON th.TourClassID = tc.TourClassID) t
PIVOT (
MAX(HotelName)
FOR TourClass IN ([Deluxe Class], [Superior Class], [Standard Class])
) AS pvt
并使用表格:
<table class="TableTour2" border="0" cellSpacing="0" cellPadding="0" width="500">
<tbody>
<tr>
<th scope=col>City</th>
<th scope=col>Deluxe Class</th>
<th scope=col>Superior Class</th>
<th scope=col>Standard Class</th>
</tr>
<%
Dim cnnPH As New System.Data.SqlClient.SqlConnection
Dim drPH As System.Data.SqlClient.SqlDataReader
Dim cmdPH As System.Data.SqlClient.SqlCommand
Try
cnnPH.ConnectionString = ConStr
cnnPH.Open()
cmdPH = New System.Data.SqlClient.SqlCommand("SELECT pvt.CityName, pvt.[Deluxe Class], pvt.[Superior Class], pvt.[Standard Class] " & _
" FROM ( SELECT " & _
" c.CityName, h.HotelName, tc.TourClass " & _
" FROM tblCity2 c " & _
" LEFT JOIN tblTourHotel2 th ON c.CityID = th.CityID " & _
" LEFT JOIN tblHotel2 h ON th.HotelID = h.HotelID " & _
" LEFT JOIN tblTourClass2 tc ON th.TourClassID = tc.TourClassID " & _
" WHERE th.TourID='1' " & _
" ) t " & _
" PIVOT ( MAX(HotelName) FOR TourClass IN ([Deluxe Class], [Superior Class], [Standard Class]) " & _
" ) AS pvt", cnnPH)
drPH = cmdPH.ExecuteReader
While drPH.Read
%>
<tr>
<th class=sub><%Response.Write(drPH("CityName"))%> </th>
<td><% Response.Write(drPH("HotelName"))%></td>
<td><% Response.Write(drPH("HotelName"))%></td>
<td><% Response.Write(drPH("HotelName"))%></td>
</tr>
<%
End While
drPH = Nothing
cnnPH.Close() : cnnPH = Nothing
Catch ex As Exception
MsgBox(ex.Message)
drPH = Nothing
cnnPH.Close() : cnnPH = Nothing
End Try
%>
</tbody>
</table>
抱歉,因为我不熟悉SQL和asp.net程序。
我在等你的帮助
非常感谢 谷沙答案 0 :(得分:1)
不要那样循环。将代码保留在标记之外。查看使用GridView或ListView或其他DataBound控件之一。