使用Webservices批量处理数据集 - C#

时间:2017-08-16 05:48:20

标签: c# multithreading web-services progress-bar backgroundworker

我有一个c#web应用程序读取excel文件将其转换为XML执行存储过程并返回执行辅助更新所需的特定格式的数据集。数据集返回18000多条记录。填充数据集后,我遍历并调用Web服务以执行对应用程序数据库的更新。我的问题是如何在较小的批次或较小的块或甚至使用进度条中执行此更新。这是调用Web服务并运行更新的函数

public string InvokeSubmitCalendarValues()
{
    try
    {
        DataWebService dataWebService = new DataWebService();
        SubmitCalendarValuesRequest submitCalendarValuesRequest = new SubmitCalendarValuesRequest();
        Credentials credentials = new Credentials();

        credentials.Username = AmplaCodeUserName;
        credentials.Password = AmplaCodeUserPassword;
        credentials.Session = "";
        submitCalendarValuesRequest.Credentials = credentials;

        string rateUnit = "";

        if (ds != null && ds.Tables.Count > 0)
        {
            foreach (DataTable dt in ds.Tables)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    SubmitCalendarValue[] values = new SubmitCalendarValue[1];
                    SubmitCalendarValue values_ = new SubmitCalendarValue();

                    values_.Name = dr["ItemName"].ToString();
                    values_.Value = dr["ItemValue"].ToString();
                    // Init DateTime object value = 2017-08-06T00:00:00.0000000+02:00
                    // We going to use DateTime ctor that takes Ticks
                    values_.StartDateTime = new System.DateTime(Convert.ToDateTime(dr["ActiveDateTime"]).Ticks);

                    if (dr["PeriodType"].ToString() != string.Empty || !(dr["Period"] is DBNull))
                    {
                        CalendarRate rate = new CalendarRate();

                        rateUnit = dr["PeriodType"].ToString();
                        rate.Count = Convert.ToInt32(dr["Period"]);

                        CalendarRateUnit cru = (CalendarRateUnit)Enum.Parse(typeof(CalendarRateUnit), rateUnit);

                        rate.Unit = cru;
                        values_.Rate = rate;
                    }

                    values[0] = values_;
                    submitCalendarValuesRequest.Values = values;

                    SubmitCalendarValuesResponse submitCalendarValuesResult = dataWebService.SubmitCalendarValues(submitCalendarValuesRequest);
                }
            }
        }
    }
    catch(Exception ex)
    {
        return ex.Message.ToString();
    }

    return "Success";
}

1 个答案:

答案 0 :(得分:0)

你可以在后端线程中完成所有这些工作,只需传递XML并调用一个线程并将消息返回给最终用户。

或者您可以将XML上传到服务器并创建一个批处理作业,然后将用户置于处理视图中。