一、准备工作:

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]带下拉列表的文本框

    控件我已经弄好了,代码比较多,所以没办法全面介绍. 一开始我是直接继承Selector类来实现,做是做出来了,不过发现性能不太好.于是,我就想着自己来实现.毕竟我是做给自己用的,也不考虑过多的东西,也 ...

  2. OpenCASCADE Gauss Integration

    OpenCASCADE Gauss Integration eryar@163.com Abstract. Numerical integration is the approximate compu ...

  3. 前端学PHP之面向对象系列第一篇——类和对象

    × 目录 [1]类 [2]成员属性[3]成员方法[4]对象[5]成员访问[6]this 前面的话 面向对象程序设计(OOP)是一种计算机编程架构.计算机程序由单个能够起到子程序作用的单元或对象组成,为 ...

  4. 【原创】.NET平台机器学习组件-Infer.NET连载(二)贝叶斯分类器

                本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html 微软Infer.NET机器学习组件文章目录:http:/ ...

  5. 解决在配置Oracle数据库连接错误问题

    项目开发使用的Oracle,但是使用了不同的版本的Oracle数据库.然后自己在电脑上安装了2个连接客户端,版本分别是: 10.2.0 12.1.0 用Toad for Oracle 9.0的时候,我 ...

  6. geotrellis使用(九)使用geotrellis进行栅格渲染

    目录 前言 图像渲染 总结 参考链接 一.前言        前面几篇文章讲解了如何使用Geotrellis进行数据处理.瓦片生成等,今天主要表一下如何使用Geotrellis进行栅格渲染.     ...

  7. 1Z0-053 争议题目解析502

    1Z0-053 争议题目解析502 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 502.A database is running In ARCHIVBXXMS mode. It ...

  8. Front End Developer Questions 前端开发人员问题(三)JavaScript部分

    问题来源:http://markyun.github.io/2015/Front-end-Developer-Questions/ 三.javascript1.介绍JavaScript的基本数据类型. ...

  9. DocX在C#中的基本操作方法

    用了一个星期把园子里2016年中有关.net的文章都看了,有些只是大致的看了一下,在看的同时也在记录一些通用的方法.发现有很多对NPOI的文档,主要是操作Excl的方法,却很少有关文档类型的方法. 在 ...

  10. ThinkPHP学习(一)

    下载3.2框架后,解压缩到web目录下面,可以看到初始的目录结构如下: 3.2版本相比之前的版本自带了一个完整的应用目录结构和默认的应用入口文件,开发人员可以在这个基 础之上灵活调整.其中, Appl ...