【发布时间】:2011-08-25 04:35:52
【问题描述】:
我已经创建了将批量数据上传到服务器(sql 数据库)的 Web 服务。
我创建了两种方法,一种是为 在服务器和其他上上传数据是为了从保存的记录中获取状态,它将继续监视 UploadingData 方法并每秒向客户端显示状态。
这里是sn-p的代码:
AJAX
//------------------------------
// Save uploaded file data in database
//------------------------------
function SaveFileData() {
DisplayMessage("Uploading bulk data from file to database, this will take time please wait...", "Loading", false);
//This will try to diaplay status of saved records in database
displayRecordStatusOn();
$.ajax({
type: "POST",
url: "SaveData.asmx/SaveFileData",
data: "{'FileName':'" + savedFileName + "', 'ClientID':'" + GetSelectedClient() + "','FileAutoID':'" + savedFileAutoID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = jQuery.parseJSON(response.d);
if (result.Success == "False") {
DisplayMessage("Error : " + result.Message, "Failed", false);
return;
}
else {
//DisplayMessage("Datatransfer process 100% completed", "Success", false);
displayRecordStatusOff();
DisplayMessage(result.Message, "Success", false);
alert("100% Done!!");
}
}
});
}
//This method called every second and get result from SaveFileData (web service method)
function displayRecordStatusOn() {
$.ajax({
type: "POST",
url: "SaveData.asmx/GetRecordsInsertStatus",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = jQuery.parseJSON(response.d);
if (result.Success == "False") {
console.log("F: " + result.Message);
}
else {
console.log("S: " + result.Message);
}
}
});
recordStatusTimer = setTimeout("displayRecordStatusOn()", 1000);
}
网络服务
string RecordStatus = "";
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string SaveFileData(string FileName, string ClientID, string FileAutoID)
{
//--HttpContext.Current.Session["CurrentCount"] = 0;
HttpContext.Current.Cache.Remove("Counter" + FileAutoID);
try
{
DataTable dtExcelData = ExcelDataLoader(FileName, ClientID, "");
long TotalRecords = dtExcelData.Rows.Count;
long CopiedRecords = 0;
using (SqlConnection cn = new SqlConnection(GetConnectionString()))
{
SqlCommand cmdCopiedRecords = new SqlCommand("SELECT COUNT(*) FROM " + string.Format(DataBankTableFormat, FileAutoID) + ";", cn);
//--HttpContext.Current.Session["TotalCount"] = TotalRecords;
HttpContext.Current.Cache.Insert("Counter" + FileAutoID, TotalRecords, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
cn.Open();
using (SqlBulkCopy copy = new SqlBulkCopy(cn))
{
copy.BatchSize = BatchSize;
copy.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
copy.NotifyAfter = BatchSize;
for (int colIndex = 0; colIndex < dtExcelData.Columns.Count; colIndex++)
{
copy.ColumnMappings.Add(colIndex, colIndex);
}
copy.DestinationTableName = string.Format(DataBankTableFormat, FileAutoID);
copy.WriteToServer(dtExcelData);
CopiedRecords = System.Convert.ToInt32(cmdCopiedRecords.ExecuteScalar());
RecordStatus = string.Format("{0} of {1} copied successfully!", CopiedRecords, TotalRecords);
}
cn.Close();
}
Response = GetResponse(true, RecordStatus);
}
catch (Exception ex)
{
Response = GetResponse(false, ex.Message);
}
return Response;
}
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetRecordsInsertStatus(string FileAutoID)
{
try
{
Response = GetResponse(true, RecordStatus(FileAutoID));
}
catch (Exception ex)
{
Response = GetResponse(false, ex.Message);
}
return Response;
}
public string RecordStatus(string FileAutoID)
{
string response = "";
//--response = string.Format("{0} of {1} copied successfully! at {2}", HttpContext.Current.Session["CurrentCount"], HttpContext.Current.Session["TotalCount"], DateTime.Now.ToString());
response = string.Format("{0} of {1} copied successfully! at {2}", HttpContext.Current.Cache["Counter" + FileAutoID] == null ? "0" : HttpContext.Current.Cache["Counter" + FileAutoID].ToString(), 786, DateTime.Now.ToString());
//return HttpContext.Current.Cache["Counter"+ClientID] == null ? "0" : HttpContext.Current.Cache["Counter"+ClientID].ToString();
return response;
}
private static void OnSqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
{
HttpContext.Current.Session["CurrentCount"] = e.RowsCopied;
}
问题:
代码工作正常,但在此方法 (SaveFileData) 之后的 SaveFileData 过程中我没有获取 RecordStatus 变量的值。
- 我有全局变量 RecordStatus,我正在更新它的值 SaveFileData 方法,我正在尝试从中获取价值 在客户端浏览器上显示的 GetRecordsInsertStatus 方法。 (但是这个 总是返回空白)
【问题讨论】:
-
使用 ASMX WebService 是绝对必要的吗?你能在这个项目中使用 .NET 4.0 吗?如果第一个问题的答案是
no而第二个问题的答案是yes我可以向您展示一个使用 PUSH 技术的示例,而不是让客户端 POLL 使用连续的 AJAX 请求。
标签: asp.net web-services jquery