一、准备工作:

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. Sql Server系列:数据控制语句

    数据控制语句用来设置.更改用户或角色的权限,包括GRANT.DENY.REVOKE等语句. GRANT用来对用户授予权限,REVOKE可用于删除已授权的权限,DENY用于防止主体通过GRANT获得特定 ...

  2. jQuery 2.0.3 源码分析core - 选择器

         声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢!      打开jQuery源码,一眼看去到处都充斥着正则表达式,jQuery框架的基础就是查询了,查询文档元素对象 ...

  3. js中用tagname和id获取元素的3种方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. document.getElementById()与 $()区别

    document.getElementById()返回的是DOM对象,而$()返回的是jQuery对象 什么是jQuery对象? ---就是通过jQuery包装DOM对象后产生的对象.jQuery对象 ...

  5. Hibernate学习之——搭建log4j日志环境

    昨天讲了Hibernate开发环境的搭建以及实现一个Hibernate的基础示例,但是你会发现运行输出只有sql语句,很多输出信息都看不见.这是因为用到的是slf4j-nop-1.6.1.jar的实现 ...

  6. 2014 Visual Studio Contact(); 直播笔记

    昨天微软干了几件了不起的事:.NET开发环境将开源.跨平台支持(Mac OS X和Linux).多设备支持(WP.Android和iOS)和Visual Studio免费(Visual Studio ...

  7. swift 的高阶函数的使用代码

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  8. grape动态PHP结构(三)——API接口

    一.app视图与控制器

  9. SQL Tuning 基础概述09 - SQL Access Advisor

    Oracle官方文档对SQL Access Advisor的描述如下: SQL Access Advisor, which is a tuning tool that provides advice ...

  10. 软件工程-构建之法 Visual Studio开发平台的安装与单元测试

    一.前言 自从开始了大三下的生活,学校开设一门课程“软件工程”,刚好我们是第一届进行课程改革,不在像以前那样背背概念,考前进行好好突击,然后考试就能过,最后毕业了发现软件工程课程到底我们在其中学习了什 ...