前言
Dynamics365中的ExecuteMultiple用来执行多个请求,比如通过控制台应用程序创建50个客户,我可以Create50次,也可以用ExecuteMultiple,1次就创建50个客户,那么两者那个创建记录耗时更短?实验如下:
1.单独创建
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Configuration;
using System.Diagnostics;
namespace Gongdh.D365.ExecuteMultiple
{
class Program
{
private static CrmServiceClient _client;
public static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
_client = new CrmServiceClient(ConfigurationManager.ConnectionStrings["Dev"].ConnectionString);
for (int i = 0; i < 50; i++)
{
Entity create_account = new Entity("account");
create_account["name"] = $"CREATE ACCOUNT {i + 1}";
_client.Create(create_account);
}
sw.Stop();
Console.WriteLine($"程序耗时:{sw.ElapsedMilliseconds}ms.");
Console.ReadKey();
}
}
}
2.ExecuteMultiple创建
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Configuration;
using System.Diagnostics;
namespace Gongdh.D365.ExecuteMultiple
{
class Program
{
private static CrmServiceClient _client;
public static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
_client = new CrmServiceClient(ConfigurationManager.ConnectionStrings["Dev"].ConnectionString);
ExecuteMultipleRequest request = new ExecuteMultipleRequest()
{
Requests = new OrganizationRequestCollection(),
Settings = new ExecuteMultipleSettings
{
ContinueOnError = false,
ReturnResponses = true
}
};
for (int i = 0; i < 50; i++)
{
Entity create_account = new Entity("account");
create_account["name"] = $"CREATE ACCOUNT {i + 1} - ExecuteMultiple";
CreateRequest createRequest = new CreateRequest()
{
Target = create_account
};
request.Requests.Add(createRequest);
}
var response = (ExecuteMultipleResponse)_client.Execute(request);//执行
sw.Stop();
Console.WriteLine($"程序耗时:{sw.ElapsedMilliseconds}ms.");
Console.ReadKey();
}
}
}
结论
ExecuteMultiple耗时更短。
标签:account,请求,c#,sw,System,ExecuteMultiple,new,using From: https://www.cnblogs.com/ynysj/p/16981842.html