如何通过使用.NET webservice的ajax调用获取大量数据? [英] How can I get large amount of data by an ajax call using .NET webservice ?
问题描述
使用ajax调用我通过'CreateReport'方法获取数据。
这是我的ajax电话:
$。ajax( {
类型: POST,
url: Service.asmx / CreateReport,
data: {'reportname':' + ReportID + ','datasourceid':' + DataSourceId [i] + ','parameter': ' +参数+ '},
contentType: application / json; charset = utf-8,
dataType: json,
async: false ,
指数:i,
成功: function (响应){
numberofquery = numberofquery + 1 ;
var Query = response.d;
// alert(查询);
store_data(查询, this .index);
function store_data(Query,i){
try {
if (Query!= null || Query!= ){
Query = JSON .parse(查询);
window [QueryName [i]] = [];
window [QueryName [i]] =查询;
}
}
catch (错误){}
}
},
async: true ,
错误: function (msg)
{
numberofquery = numberofquery + 1 ;
CustomAlertShow( 错误);
},
complete: function (){
// GenerateAllReports();
// console.log(numq =+ numberofquery +,totalq =+ totalquery);
if ( numberofquery == totalquery)GenerateWithReloadedData();
},
});
这是我服务的方法。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string CreateReport( string reportname, string datasourceid, string 参数)
{
try
{
DataSet dataSet1 = new DataSet();
SqlConnection cn1 = new SqlConnection(ConStr);
string queryString = 从dbo.ReportDataSourceDataModel中选择ReportID,DataSourceID
SqlDataAdapter daGlobalClass = new SqlDataAdapter(queryString,cn1);
cn1.Open();
daGlobalClass.Fill(dataSet1, myDataTable) ;
cn1.Close();
return JsonConvert.SerializeObject(dataSet1.Tables [ 0 ]);
}
catch (SqlException ex)
{
< span class =code-keyword> return - 错误: + DisplaySqlErrors(前);
}
}
一般情况下工作正常当dataset1有大约100000个数据时。但是这项服务不适用于大量数据。
如何使用此ajax调用通过此webservice anad获取超过百万的数据?
。 ajax({
类型: POST,
url: Service.asmx / CreateReport,
data: {'reportname':' + ReportID + ','datasourceid':' + DataSourceId [i] + ','参数':' +参数+ '},
contentType: application / json; charset = utf-8,
dataType: json,
async:false ,
index:i,
成功: function (回复){
numberofquery = numberofquery + 1 ;
var Query = response.d;
// alert(查询);
store_data(查询, this .index);
function store_data(Query,i){
try {
if (Query!= null || Query!= ){
Query = JSON .parse(查询);
window [QueryName [i]] = [];
window [QueryName [i]] =查询;
}
}
catch (错误){}
}
},
async: true ,
错误: function (msg)
{
numberofquery = numberofquery + 1 ;
CustomAlertShow( 错误);
},
complete: function (){
// GenerateAllReports();
// console.log(numq =+ numberofquery +,totalq =+ totalquery);
if ( numberofquery == totalquery)GenerateWithReloadedData();
},
});
这是我服务的方法。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string CreateReport( string reportname, string datasourceid, string 参数)
{
try
{
DataSet dataSet1 = new DataSet();
SqlConnection cn1 = new SqlConnection(ConStr);
string queryString = 从dbo.ReportDataSourceDataModel中选择ReportID,DataSourceID
SqlDataAdapter daGlobalClass = new SqlDataAdapter(queryString,cn1);
cn1.Open();
daGlobalClass.Fill(dataSet1, myDataTable) ;
cn1.Close();
return JsonConvert.SerializeObject(dataSet1.Tables [ 0 ]);
}
catch (SqlException ex)
{
< span class =code-keyword> return - 错误: + DisplaySqlErrors(前);
}
}
一般情况下工作正常当dataset1有大约100000个数据时。但是这项服务不适用于大量数据。
如何使用此ajax调用通过此webservice anad获取超过百万的数据?
这有趣!查看异常堆栈,我看到错误不在客户端,而是在将数据从datatable转换为json字符串时在服务器上。这意味着JsonConvert.SerializeObject(dataSet1.Tables [0]);抛出错误。
有很多选项可以选择摆脱这个错误:
1.将数据保存为文件,并允许用户将文件下载到本地计算机中。这将允许他在他的机器中查看数据。
2.实施分页,以便在客户端和服务器之间只交换一小组信息。 (例如gmail使用分页。)
3.流式传输数据(查看缓冲响应)
有关详细信息,请参阅以下链接:
https://msdn.microsoft。 com / en-in / library / aa528818.aspx [ ^ ]
http://www.drdobbs.com/windows/parsing-big-records-with-jsonnet/240165316?pgno=2 [ ^ ]
http://www.newtonsoft.com/json/help/html/ Performance.htm [ ^ ]
如解决方案3中所述,这些是您唯一的选择。
发生的事情是您转换为JSON的对象被序列化为一个字符串,这个字符串对于.NET来说太大了。最大的任何对象(包括一个字符串)可以是2GB大小。
你只是返回太多数据来序列化为一个字符串并返回到客户端。
Using an ajax call I get data by'CreateReport' method.
Here is my ajax call:
$.ajax({
type: "POST",
url: "Service.asmx/CreateReport",
data: "{'reportname':'" + ReportID + "','datasourceid':'" + DataSourceId[i] + "','parameter':'" + Parameter + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
index: i,
success: function (response) {
numberofquery = numberofquery + 1;
var Query = response.d;
//alert(Query);
store_data(Query, this.index);
function store_data(Query, i) {
try {
if (Query != null || Query != "") {
Query = JSON.parse(Query);
window[QueryName[i]] = [];
window[QueryName[i]] = Query;
}
}
catch (err) { }
}
},
async: true,
error: function (msg)
{
numberofquery = numberofquery + 1;
CustomAlertShow("Error");
},
complete: function () {
//GenerateAllReports();
//console.log("numq=" + numberofquery + ",totalq=" + totalquery);
if (numberofquery == totalquery) GenerateWithReloadedData();
},
});
And here is my method in service.
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string CreateReport(string reportname, string datasourceid, string parameter)
{
try
{
DataSet dataSet1 = new DataSet();
SqlConnection cn1 = new SqlConnection(ConStr);
string queryString = "Select ReportID, DataSourceID from dbo.ReportDataSourceDataModel "
SqlDataAdapter daGlobalClass = new SqlDataAdapter(queryString, cn1);
cn1.Open();
daGlobalClass.Fill(dataSet1, "myDataTable");
cn1.Close();
return JsonConvert.SerializeObject(dataSet1.Tables[0]);
}
catch (SqlException ex)
{
return "-ERR: " + DisplaySqlErrors(ex);
}
}
In general it works fine when dataset1 has around 100000 data. But this service doesn't work for large amount of data.
How can I get over million data through this webservice anad using this ajax call ?
.ajax({ type: "POST", url: "Service.asmx/CreateReport", data: "{'reportname':'" + ReportID + "','datasourceid':'" + DataSourceId[i] + "','parameter':'" + Parameter + "'}", contentType: "application/json; charset=utf-8", dataType: "json", async: false, index: i, success: function (response) { numberofquery = numberofquery + 1; var Query = response.d; //alert(Query); store_data(Query, this.index); function store_data(Query, i) { try { if (Query != null || Query != "") { Query = JSON.parse(Query); window[QueryName[i]] = []; window[QueryName[i]] = Query; } } catch (err) { } } }, async: true, error: function (msg) { numberofquery = numberofquery + 1; CustomAlertShow("Error"); }, complete: function () { //GenerateAllReports(); //console.log("numq=" + numberofquery + ",totalq=" + totalquery); if (numberofquery == totalquery) GenerateWithReloadedData(); }, });
And here is my method in service.
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string CreateReport(string reportname, string datasourceid, string parameter) { try { DataSet dataSet1 = new DataSet(); SqlConnection cn1 = new SqlConnection(ConStr); string queryString = "Select ReportID, DataSourceID from dbo.ReportDataSourceDataModel " SqlDataAdapter daGlobalClass = new SqlDataAdapter(queryString, cn1); cn1.Open(); daGlobalClass.Fill(dataSet1, "myDataTable"); cn1.Close(); return JsonConvert.SerializeObject(dataSet1.Tables[0]); } catch (SqlException ex) { return "-ERR: " + DisplaySqlErrors(ex); } }
In general it works fine when dataset1 has around 100000 data. But this service doesn't work for large amount of data.
How can I get over million data through this webservice anad using this ajax call ?
Thats interesting! Looking at the exception stack I see that the error is not on the client side but on the server while converting your data from datatable to json string. This means "JsonConvert.SerializeObject(dataSet1.Tables[0]);" is throwing the error.
There are quite a few options you can choose to get rid of this error:
1. Save the data as a file and allow the user to download the file into his local machine. This will allow him to see the data in his machine.
2. Implement pagination so that at a time only a small set of information is exchanged between client and server. (E.g. gmail uses pagination.)
3. Stream the data (check Buffered response)
See the below mentioned links for more information:
https://msdn.microsoft.com/en-in/library/aa528818.aspx[^]
http://www.drdobbs.com/windows/parsing-big-records-with-jsonnet/240165316?pgno=2[^]
http://www.newtonsoft.com/json/help/html/Performance.htm[^]
As specified in Solution 3, those are your only options.
What's happening is that the object you're converting to JSON is being serialized into a string that is way too big for .NET to hold. The largest any object (including a string) can be is 2GB in size.
You're simply returning too much data to be serialized into a string and returned to the client.
这篇关于如何通过使用.NET webservice的ajax调用获取大量数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!