一、准备工作:

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+条记录。

CRM基于.NET的增删改查的更多相关文章

  1. Mybatis_3.基于注解的增删改查

    1.实体类User.java public class User { private int id; private String name; private int age; //getter.se ...

  2. ASP.NET Web API基于OData的增删改查,以及处理实体间关系

    本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先是比较典型的一对多关系,Supplier和Product. public class Product { ...

  3. [转]ASP.NET Web API基于OData的增删改查,以及处理实体间关系

    本文转自:http://www.cnblogs.com/darrenji/p/4926334.html 本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先 ...

  4. Node.js、express、mongodb 入门(基于easyui datagrid增删改查)

    前言 从在本机(win8.1)环境安装相关环境到做完这个demo大概不到两周时间,刚开始只是在本机安装环境并没有敲个Demo,从周末开始断断续续的想写一个,按照惯性思维就写一个增删改查吧,一方面是体验 ...

  5. Mybatis_2.基于XML的增删改查

    1.实体类User.java public class User { private int id; private String name; private int age; //getter.se ...

  6. 基于django做增删改查组件,分页器组件

    增删改查组件 一.Djangoadmin的启发 二.基于Djangoadmin实现数据的增删改查 分页器组件 分页器组件的介绍以及源码解读 补充:源码下载,

  7. RESTful最佳实践之基于 jersey 的增删改查

    jersey-rest-demo 增删改查 项目地址:https://github.com/CoderDream/jersey-rest-demo 源代码:http://download.csdn.n ...

  8. Mybatis之基于XML的增删改查

    这里先吐槽下,写的半天的东西,IE浏览器弹出调试窗口导致写的东西全部没保存,搞得我还要重新用谷歌写,思路全没了,fuck. 前面学习了下spring的DAO层,说起DAO层,那ORM肯定是少不了的,O ...

  9. Django基于类的增删改查,简单逻辑都不用写

    Django是Python中一个非常牛逼的web框架,他帮我们做了很多事,里边也提前封装了很多牛逼的功能,用起来简直不要太爽,在写网站的过程中,增删改查这几个基本的功能我们是经常会用到,Django把 ...

随机推荐

  1. 【求助】WPF 在XP下 有的Textbox光标会消失

    最近做个项目,一直有一个问题没有解决,就是在XP下,有的Textbox里在文本框里没有东西的时候,会没有光标.不同的XP机器,失去光标的Textbox也不一样. 各位大师看下面的三张图,当Textbo ...

  2. PHP+MySQL代码部署在Linux(Ubuntu)上注意事项

    最近帮同学做一个网站,同学买的是阿里云服务器,Linux发行版是Ubuntu12.04.我在本地把程序写好,都调试好了.然后他让我自己发布和部署.之前在大学里上操作系统课程时,也用过一段时间的Ubun ...

  3. 图的广度优先搜索(BFS)

    把以前写过的图的广度优先搜索分享给大家(C语言版) #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 ...

  4. ASP.NET MVC Authorization 自定义跳转

    应用场景:在 ASP.NET MVC 应用程序中,需要对用户身份权限进行验证,比如没有登录或者不符合权限的用户,访问 Action 的时候,跳转到指定页面. 重写 Authorize: public ...

  5. 如何添加并设置远程桌面(RD)授权服务器

    上一篇日志中介绍了如何将现成的远程桌面授权服务器添加到对应的远程桌面回话主机中. 本篇日志将引导您如何添加配置相应的远程桌面授权服务器,这样就可以根据所购买的授权类型和授权级别添加需要甚至" ...

  6. T-SQL CROSS APPLY、MERGE

    写在前面 刚才看项目里一个存储过程,也是好长时间没有使用Sql Server2008了,好多写法和函数感觉到陌生,这就遇到了CROSS APPLY 和MERGE的语法,两者之前完全没接触过. 所以专门 ...

  7. Android 程序打包及签名

    为什么要签名??? 开发Android的人这么多,完全有可能大家都把类名,包名起成了一个同样的名字,这时候如何区分?签名这时候就是起区分作用的. 由于开发商可能通过使用相同的Package Name来 ...

  8. HTML基础知识汇总

    前言 一直想总结一下,苦于没有时间,正好看到了一个总结了不错的博客,我就在他的基础上进行一下测试并总结,原博地址:http://www.cnblogs.com/wanghzh/p/5805587.ht ...

  9. Elasticsearch 教程--搜索

    搜索 – 基本工具 到目前为止,我们已经学习了Elasticsearch的分布式NOSQL文档存储,我们可以直接把JSON文档扔到Elasticsearch中,然后直接通过ID来进行调取.但是Elas ...

  10. 基于Treeview控件遍历本地磁盘

    一.前言 Treeview控件常用于遍历本地文件信息,通常与Datagridview与ImageList搭配.ImageList控件用于提供小图片给TreeView控件,DatagridView通常显 ...