我有一个页面需要单击表中的数字,然后相应地从数据库中读取另一个表。我曾经使用 ASP gridview 来生成第二个表。然后我尝试使用jquery ajax。当我需要请求 20,000 条记录时,jquery ajax 非常慢(加载大约 6 秒),而 asp gridview 则要快得多。
ASPX.VB
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
<WebMethod()> _
Public Shared Function GetOrderDetail(ByVal id As String) As String
Dim orderNum As Integer = Integer.Parse(id)
Dim sbTable As New StringBuilder
Dim sql As New StringBuilder
sql.Append("SELECT oh.[ORDERNO], od.[STYLE], s.[STYLEDESC],od.[COLOR], c.[COLORDESC], od.[SIZE],")
sql.Append(" SUM(CONVERT(DECIMAL(12,0),od.[TOTALQTY])) AS 'Item_Total', och.[TRACKINGNO]")
sql.Append(" FROM [OrderHeader] AS oh INNER JOIN [test1] AS ocd")
sql.Append(" ON oh.[ORDERNO] = ocd.[ORDERNO] INNER JOIN [test2] AS och")
sql.Append(" ON ocd.[FKEY] = och.[PKEY] INNER JOIN [test3] AS od")
sql.Append(" ON oh.[ORDERNO] = od.[ORDERNO] INNER JOIN [test4] as s")
sql.Append(" ON s.[STYLE] = od.[STYLE] INNER JOIN [test5] AS c")
sql.Append(" ON c.[COLOR] = od.[COLOR]")
sql.Append(" WHERE od.[ORDERNO] =")
sql.Append(orderNum)
sql.Append(" GROUP BY oh.[ORDERNO], od.[STYLE], s.[STYLEDESC],od.[COLOR], c.[COLORDESC], od.[SIZE], och.[TRACKINGNO]")
sql.Append("select [ORDERNO] from [oabcd].[dbo].[OrderHeader] where [ORDERNO] =")
sql.Append(orderNum)
Dim connMain As Data.SqlClient.SqlConnection
Dim cmdMain As Data.SqlClient.SqlCommand
Dim rdrMain As Data.SqlClient.SqlDataReader
connMain = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("SsssConnectionString").ConnectionString)
connMain.Open()
cmdMain = New Data.SqlClient.SqlCommand(sql.ToString(), connMain)
rdrMain = cmdMain.ExecuteReader()
Dim bAlt As Boolean = True
If rdrMain.HasRows Then
sbTable.Append("<table class = 'orderDetailTable'>")
sbTable.Append("<thead>")
sbTable.Append("<tr>")
sbTable.Append("<th>")
sbTable.Append("Order Number")
sbTable.Append("</th>")
sbTable.Append("<th>")
sbTable.Append("Item")
sbTable.Append("</th>")
sbTable.Append("<th>")
sbTable.Append("Color")
sbTable.Append("</th>")
sbTable.Append("<th>")
sbTable.Append("Size")
sbTable.Append("</th>")
sbTable.Append("<th>")
sbTable.Append("Item Total")
sbTable.Append("</th>")
sbTable.Append("<th>")
sbTable.Append("Tracking Number")
sbTable.Append("</th>")
sbTable.Append("</tr>")
sbTable.Append("</thead>")
sbTable.Append("<tbody>")
While rdrMain.Read()
sbTable.Append("<tr>")
sbTable.Append("<td>")
sbTable.Append(rdrMain.Item("ORDERNO"))
sbTable.Append("</td>")
sbTable.Append("<td>")
sbTable.Append(rdrMain.Item("STYLEDESC"))
sbTable.Append("</td>")
sbTable.Append("<td>")
sbTable.Append(rdrMain.Item("COLORDESC"))
sbTable.Append("</td>")
sbTable.Append("<td>")
sbTable.Append(rdrMain.Item("Size"))
sbTable.Append("</td>")
sbTable.Append("<td>")
sbTable.Append(rdrMain.Item("Item_Total"))
sbTable.Append("</td>")
sbTable.Append("<td>")
sbTable.Append(rdrMain.Item("TRACKINGNO"))
sbTable.Append("</td>")
sbTable.Append("</tr>")
End While
sbTable.Append("</tbody>")
sbTable.Append("</table>")
End If
rdrMain.Close()
cmdMain.Dispose()
connMain.Close()
connMain.Dispose()
Return sbTable.ToString()
End Function
JS
$('.orderNumber').on('click', function () {
var orderNum = $(this).text();
console.log(orderNum);
$.ajax({
type: "POST",
url: "Order.aspx/GetOrderDetail",
data: "{'id' :'" + orderNum + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
$('#orderDetail').html(result.d);
$('.orderDetailTable').dataTable({
});
},
error: function (xhr, status, error) {
// Display a generic error for now.
alert("Ajax Read Detail Error!");
}
});
});
我返回了一个非常大的字符串(包含 20,000 条记录)。我的问题是:有没有办法像返回其他类型一样改进代码?GetOrderDetail()
或者不返回任何东西只是从asp函数生成html ?有什么可以使请求更快的吗?谢谢。