一、准备工作:
1.添加 microsoft.crm.sdk.proxy.dll和microsoft.xrm.sdk.dll 引用到项目中!并引用以下using!
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.ServiceModel.Description;
using Microsoft.Crm.Sdk.Messages;
二、增删改查
1.CRM连接方法;
Uri uri = new Uri("http://192.168.112.107/mzj-crm2011/XRMServices/2011/Organization.svc");//CRM发表地址
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("test", "7654321", "hz.sh.cn");//账号,密码,域名
OrganizationServiceProxy _service = new OrganizationServiceProxy(uri, null, clientCredentials, null);
_service.EnableProxyTypes();
2.增(向CRM插入数据),默认你将要插入的数据读取到DataTable;
Entity total = new Entity("new_yzsjz_street_total");//CRM的实体名称必须小写
total["new_street_name"] = new_streetname;
total["new_yzsjz_person"] = dt.Rows[i]["jzrs"].ToString();
total["new_countstatus"] = dt.Rows[i]["cishu"].ToString();
total["new_year"] = dt.Rows[i]["yy"].ToString();
total["new_check_type"] = new OptionSetValue();//多选项
total["new_suspicious_person"] = dt.Rows[i]["kyrs"].ToString();
total["new_sumallamt"] = dt.Rows[i]["jine"].ToString();
total["new_month"] = dt.Rows[i]["mm"].ToString(); string sql1 = string.Format("select TeamId from Team where Name='{0}'", new_streetname);
SqlCommand cmd1 = new SqlCommand(sql1, con);
con.Open();
SqlDataAdapter sa1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
sa1.Fill(ds1);
dt1 = ds1.Tables[];
con.Close();
string guid = dt1.Rows[]["teamid"].ToString();//获取负责人的Guid //创建记录并为该记录添上负责人
Guid hh = _service.Create(total);
Guid aa = new Guid(guid);
AssignRequest assignRequest = new AssignRequest();
assignRequest.Assignee = new EntityReference("team", aa);//此处为团队,若负责人为用户则为systemuser
assignRequest.Target = new EntityReference("new_yzsjz_street_total", hh);//实体名称
_service.Execute(assignRequest);
增
3.删(删除CRM中的记录);
string new_id = dtjh.Rows[i]["new_id"].ToString();
//通过条件查询出记录的GUID
QueryExpression prosal = new QueryExpression("new_marry_person_reg");//实体名称
prosal.ColumnSet.AddColumn("new_id");//条件
FilterExpression filt = prosal.Criteria.AddFilter(LogicalOperator.And);//条件逻辑 and 或者 or
filt.AddCondition("new_id", ConditionOperator.Equal, new_id);//逻辑
EntityCollection entcol = _service.RetrieveMultiple(prosal);
if (new_id!="")
{
if (entcol.Entities.Count>)
{
for (int j = ; j < entcol.Entities.Count; j++)
{
Entity entiy = new Entity("new_marry_person_reg");
entiy.Id = entcol.Entities[j].Id;//记录GUID
DeleteRequest deleteRequest = new DeleteRequest();
deleteRequest.Target = new EntityReference("new_marry_person_reg", entiy.Id);
DeleteResponse deleteResponse = (DeleteResponse)_service.Execute(deleteRequest);//删除方法
}
}
}
删
Tips:删除也可直接从数据库中读取到记录的GUID,即字段名称为“实体名+ID”。再放入以上的DeleteRequest方法中!
4.改(修改CRM中的记录的数据);
UpdateRequest uq = new UpdateRequest();
Entity contact2 = new Entity("new_yzsjz_daily_settlement");
contact2.Id = guidmtd;//通过查询获取得记录的Guid
if (guidym != "")
{
//CRM中lookup类型字段的赋值方法
Guid new_tomontha = new Guid(guidym);
EntityReference newguid = new EntityReference("new_jz_person", new_tomontha);
contact2["new_tomonth"] = newguid;
}
uq.Target = contact2;
UpdateResponse updateResponse = (UpdateResponse)_service.Execute(uq);
改
5.查(查询CRM中的记录);
//等同于sql语句
QueryExpression prosal = new QueryExpression("new_yzsjz_all_settlement");//当月
prosal.ColumnSet.AddColumn("new_insureid");
prosal.ColumnSet.AddColumn("new_year");
prosal.ColumnSet.AddColumn("new_month");
FilterExpression filt = prosal.Criteria.AddFilter(LogicalOperator.And);
filt.AddCondition("new_insureid", ConditionOperator.Equal, new_insureid);//身份证
filt.AddCondition("new_year", ConditionOperator.Equal, year);//年
filt.AddCondition("new_month", ConditionOperator.Equal, month);//月
EntityCollection entcol = _service.RetrieveMultiple(prosal);
if (new_insureid != "" && year != "" && month != "")
{
if (entcol.Entities.Count > )
{
Guid guid1 = entcol.Entities[].Id;//记录GUID
Entity entiy2 = _service.Retrieve
("new_yzsjz_all_settlement", guid1, new ColumnSet(true));
string aa=entiy2.Attributes["aa"].ToString()//该记录中的aa数据 }
}
查
Tips:建议若需查询的记录太多,还是用SQL查询方法较好,CRM自带的方法只会查出5000+条记录。