一、准备工作:

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. Js添加消息提示数量

    接到个新需求,类似以下这种需求,得把它封装成一个插件 后端给返回一个这种数据 var data = [ { key:"020506", num:5 }, { key:"0 ...

  2. 如何转型成为SQL Server DBA

        本篇PPT是我在2015 MVP OPEN Day Comunity Camp上分享的课程.之所以选择这个主题是因为有很多人曾经问过这方面的问题,而与之相关的主题却少之又少,因此我希望将自己的 ...

  3. jQuery 2.0.3 源码分析 钩子机制 - 属性操作

    jQuery提供了一些快捷函数来对dom对象的属性进行存取操作. 这一部分还是比较简单的. 根据API这章主要是分解5个方法 .attr()   获取匹配的元素集合中的第一个元素的属性的值  或 设置 ...

  4. ASP.NET MVC5 网站开发实践(二) Member区域–我的咨询列表及添加咨询

    上次把咨询的架构搭好了,现在分两次来完成咨询:1.用户部分,2管理部分.这次实现用户部分,包含两个功能,查看我的咨询和进行咨询. 目录: ASP.NET MVC5 网站开发实践 - 概述 ASP.NE ...

  5. TSQL语句

    1.创建数据库create database test3;2.删除数据库drop database test3;3.创建表create table test(    code varchar(20), ...

  6. ASP.NET sync over async(异步中同步,什么鬼?)

    async/await 是我们在 ASP.NET 应用程序中,写异步代码最常用的两个关键字,使用它俩,我们不需要考虑太多背后的东西,比如异步的原理等等,如果你的 ASP.NET 应用程序是异步到底的, ...

  7. Microsoft.Office.Interop.Excel的用法以及利用Microsoft.Office.Interop.Excel将web页面转成PDF

    1.常见用法           using Microsoft.Office.Interop.Excel; 1)新建一个Excel ApplicationClass ExcelApp = New A ...

  8. js修改不了input的值

    奇怪的input 今天想做一个通过点击按钮,修改input值的控件,但是点击按钮后,input值变成修改的值后又变回了原来的值,百思不得其解,代码如下 <form> <div cla ...

  9. MongoDB初学

    参考:MongoDB 教程 | 菜鸟教程 安装 1.下载安装,从官网下载winows安装包,安装到d盘(注意路径) 2.由于我安装在d盘,所以在d盘创建文件夹data,里面创建db文件夹 3.启动方法 ...

  10. Bootstrap 3.0正式版发布!

    Bootstrap是Twitter推出的一个开源的用于前端开发的工具包,包含了丰富的Web组件.根据这些组件,开发者可以快速的搭建一个漂亮.功能完备的网站.在经过Bootstrap 3 RC版的测试和 ...