CRM基于.NET的增删改查
一、准备工作:
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;
1 1 Entity total = new Entity("new_yzsjz_street_total");//CRM的实体名称必须小写 2 2 total["new_street_name"] = new_streetname; 3 3 total["new_yzsjz_person"] = dt.Rows[i]["jzrs"].ToString(); 4 4 total["new_countstatus"] = dt.Rows[i]["cishu"].ToString(); 5 5 total["new_year"] = dt.Rows[i]["yy"].ToString(); 6 6 total["new_check_type"] = new OptionSetValue(100000000);//多选项 7 7 total["new_suspicious_person"] = dt.Rows[i]["kyrs"].ToString(); 8 8 total["new_sumallamt"] = dt.Rows[i]["jine"].ToString(); 9 9 total["new_month"] = dt.Rows[i]["mm"].ToString(); 10 10 11 11 string sql1 = string.Format("select TeamId from Team where Name='{0}'", new_streetname); 12 12 SqlCommand cmd1 = new SqlCommand(sql1, con); 13 13 con.Open(); 14 14 SqlDataAdapter sa1 = new SqlDataAdapter(cmd1); 15 15 DataTable dt1 = new DataTable(); 16 16 DataSet ds1 = new DataSet(); 17 17 sa1.Fill(ds1); 18 18 dt1 = ds1.Tables[0]; 19 19 con.Close(); 20 20 string guid = dt1.Rows[0]["teamid"].ToString();//获取负责人的Guid 21 21 22 22 //创建记录并为该记录添上负责人 23 23 Guid hh = _service.Create(total); 24 24 Guid aa = new Guid(guid); 25 25 AssignRequest assignRequest = new AssignRequest(); 26 26 assignRequest.Assignee = new EntityReference("team", aa);//此处为团队,若负责人为用户则为systemuser 27 27 assignRequest.Target = new EntityReference("new_yzsjz_street_total", hh);//实体名称 28 28 _service.Execute(assignRequest);
3.删(删除CRM中的记录);
1 string new_id = dtjh.Rows[i]["new_id"].ToString(); 2 //通过条件查询出记录的GUID 3 QueryExpression prosal = new QueryExpression("new_marry_person_reg");//实体名称 4 prosal.ColumnSet.AddColumn("new_id");//条件 5 FilterExpression filt = prosal.Criteria.AddFilter(LogicalOperator.And);//条件逻辑 and 或者 or 6 filt.AddCondition("new_id", ConditionOperator.Equal, new_id);//逻辑 7 EntityCollection entcol = _service.RetrieveMultiple(prosal); 8 if (new_id!="") 9 { 10 if (entcol.Entities.Count>1) 11 { 12 for (int j = 1; j < entcol.Entities.Count; j++) 13 { 14 Entity entiy = new Entity("new_marry_person_reg"); 15 entiy.Id = entcol.Entities[j].Id;//记录GUID 16 DeleteRequest deleteRequest = new DeleteRequest(); 17 deleteRequest.Target = new EntityReference("new_marry_person_reg", entiy.Id); 18 DeleteResponse deleteResponse = (DeleteResponse)_service.Execute(deleteRequest);//删除方法 19 } 20 } 21 }
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 > 0) { Guid guid1 = entcol.Entities[0].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+条记录。
昨日龌龊不足夸,今日放荡思无涯。
--华芯