一、首先新建一个控制台程序。命名为TestCol。

二、打开App.config在里面加入,数据库和CRM连接字符串

<connectionStrings>
<add name="SqlServerConnString" connectionString="server=IP地址;database=数据库名称;uid=sa;pwd=密码"/>
<add name="CrmConnnectionString" connectionString="Url=http://IP地址/组织名;Username=用户名;Password=密码;Domain=域;"/>
</connectionStrings>

三、打开Program.cs写代码。主要代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.Xrm.Client;//
using System.Data;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Threading.Tasks; namespace TestCol
{
public class Program
{
static string CRMConnectionPath = string.Empty; // CRM连接字符串
static SqlConnection sqlConnection = new SqlConnection(); // 中间库连接字符串
static void Main(string[] args)
{
InitializeConfig();//初始化链接
CrmConnection connection = CrmConnection.Parse(CRMConnectionPath);
using (CrmOrganizationServiceContext orgservice = new CrmOrganizationServiceContext(connection))
{
getNew_categorytate(orgservice);
}
} //逻辑方法
protected static void getNew_categorytate(IOrganizationService service)
{
string sqlstr = "select * from Check_Buget_Sumtable where new_message1=0";//所有未同步的数据
DataTable dt = new DataTable();
SQLExecuteData(sqlstr, sqlConnection,dt);
for (int i = ; i < dt.Rows.Count; i++)
{
string New_buget_name = !string.IsNullOrEmpty(dt.Rows[i]["New_buget_name"].ToString()) ? dt.Rows[i]["New_buget_name"].ToString() : string.Empty;//预算费用项名称
string new_expenseitem_name = !string.IsNullOrEmpty(dt.Rows[i]["new_expenseitem_name"].ToString()) ? dt.Rows[i]["new_expenseitem_name"].ToString() : string.Empty;//费用项目名称
string new_bedgetsheet_name = !string.IsNullOrEmpty(dt.Rows[i]["new_bedgetsheet_name"].ToString()) ? dt.Rows[i]["new_bedgetsheet_name"].ToString() : string.Empty;//预算期间名称
string new_bugetunit_name = !string.IsNullOrEmpty(dt.Rows[i]["new_bugetunit_name"].ToString()) ? dt.Rows[i]["new_bugetunit_name"].ToString() : string.Empty;//提交部门名称
string new_sort_name = !string.IsNullOrEmpty(dt.Rows[i]["new_sort_name"].ToString()) ? dt.Rows[i]["new_sort_name"].ToString() : string.Empty;//所属品类名称
string new_type_name = !string.IsNullOrEmpty(dt.Rows[i]["new_type_name"].ToString()) ? dt.Rows[i]["new_type_name"].ToString() : string.Empty;//品类名称
string new_typecode_name = !string.IsNullOrEmpty(dt.Rows[i]["new_typecode_name"].ToString()) ? dt.Rows[i]["new_typecode_name"].ToString() : string.Empty;//品类编码
string new_month = !string.IsNullOrEmpty(dt.Rows[i]["new_month"].ToString()) ? dt.Rows[i]["new_month"].ToString() : string.Empty;//月份
decimal new_usabled_buget = Convert.ToDecimal(dt.Rows[i]["new_usabled_buget"].ToString());//预计全年可用预算
decimal new_sum_buget = Convert.ToDecimal(dt.Rows[i]["new_sum_buget"].ToString());//累计实现预算
string new_message1 = !string.IsNullOrEmpty(dt.Rows[i]["new_message1"].ToString()) ? dt.Rows[i]["new_message1"].ToString() : ""; EntityCollection encols = getBuget(service, New_buget_name);
foreach (Entity item in encols.Entities)
{
EntityCollection encol = getCategorytate(service, new_typecode_name, new_type_name, item.Id, new_month);
foreach (Entity item1 in encol.Entities)
{
try
{
updateCategorytate(service, item1.Id, new_usabled_buget, new_sum_buget);
updateMessage(New_buget_name, new_typecode_name, "", new_month);
}
catch (Exception ex)
{
updateMessage(New_buget_name, new_typecode_name, "", new_month);
}
}
} }
} //修改预算统计表的message1的值为2
public static void updateMessage(string New_buget_name,string new_typecode_name,string num,string month)
{
string sql = string.Format("update Check_Buget_Sumtable set new_message1={0} where New_buget_name='{1}' and new_typecode_name='{2}' and new_month='{3}'", num, New_buget_name, new_typecode_name, month);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
int resultSet = SQLExecuteQuery(sqlConnection, cmd);
} //查出预算费用项
protected static EntityCollection getBuget(IOrganizationService service, string New_buget_name)
{
QueryByAttribute query = new QueryByAttribute("new_buget");
query.ColumnSet = new ColumnSet("new_expenseitem", "new_bedgetsheet", "new_bugetunit", "new_sort");
query.AddAttributeValue("statecode", );
query.AddAttributeValue("new_name", New_buget_name);//预算费用项名称
EntityCollection encols = service.RetrieveMultiple(query);
return encols;
} //查品类率表
protected static EntityCollection getCategorytate(IOrganizationService service, string new_sn,string new_name,
Guid new_buget,string month)
{
int intmonth = ;
switch (month)
{
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
case "":
intmonth =;
break;
}
QueryByAttribute query = new QueryByAttribute("new_categorytate");
query.ColumnSet = new ColumnSet();
query.AddAttributeValue("statecode", );
query.AddAttributeValue("new_sn", new_sn);//产品品类编号
query.AddAttributeValue("new_name", new_name);//产品品类名称
query.AddAttributeValue("new_buget", new_buget);
query.AddAttributeValue("new_bugetmonth", intmonth); EntityCollection encols = service.RetrieveMultiple(query);
return encols;
} //更新品类率表的new_expectedannualbudget【预计全年可用预算】和new_cumulativeactualbudget【累计实现预算】
protected static void updateCategorytate(IOrganizationService service,Guid new_categorytateid,
decimal new_usabled_buget, decimal new_sum_buget)
{
Entity updateEntity = new Entity("new_categorytate");
if (new_categorytateid != Guid.Empty)
{
updateEntity[updateEntity.LogicalName+"id"] = new_categorytateid;
updateEntity["new_expectedannualbudget"] = new Money(new_usabled_buget);
updateEntity["new_cumulativeactualbudget"] = new Money(new_sum_buget);
service.Update(updateEntity);
}
} //初始化连接信息
protected static void InitializeConfig()
{
sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ToString());
CRMConnectionPath = ConfigurationManager.ConnectionStrings["CrmConnnectionString"].ToString();
} //查询数据方法
protected static void SQLExecuteData(string CommandText, SqlConnection conn, DataTable dataTable)
{
DateTime a = DateTime.Now;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(CommandText, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dataTable);
}
catch { }
finally
{
conn.Close();
}
} // 插入、更新、删除数据
protected static int SQLExecuteQuery(SqlConnection conn, SqlCommand cmd)
{
DateTime a = DateTime.Now;
int i = ;
try
{
conn.Open();
cmd.Connection = conn;
i = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
return i;
} }
}

控制台程序实现利用CRM组织服务和SqlConnection对数据库中数据的增删改查操作的更多相关文章

  1. 使用JDBC分别利用Statement和PreparedStatement来对MySQL数据库进行简单的增删改查以及SQL注入的原理

    一.MySQL数据库的下载及安装 https://www.mysql.com/ 点击DOWNLOADS,拉到页面底部,找到MySQL Community(GPL)Downloads,点击 选择下图中的 ...

  2. AD 域服务简介(三)- Java 对 AD 域用户的增删改查操作

    博客地址:http://www.moonxy.com 关于AD 域服务器搭建及其使用,请参阅:AD 域服务简介(一) - 基于 LDAP 的 AD 域服务器搭建及其使用 Java 获取 AD 域用户, ...

  3. PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码

    PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...

  4. Go微服务框架go-kratos实战03:使用 gorm 实现增删改查操作

    一.简介 在上一篇文章 go-kratos实战02 中,详细介绍了用 kratos 编写项目代码的步骤.这篇就在上篇基础上,再结合 Go 数据库操作库 gorm 一步一步来实现一个简单的增删改查操作. ...

  5. mysql详解常用命令操作,利用SQL语句创建数据表—增删改查

    关系型数据库的核心内容是 关系 即 二维表 MYSQL的启动和连接show variables; [所有的变量] 1服务端启动 查看服务状态 sudo /etc/init.d/mysql status ...

  6. JavaWeb程序利用Servlet的对SQLserver增删改查操作

    声明:学了几天终于将增删改查的操作掌握了,也发现了一些问题,所以总结一下. 重点:操作数据库主要用的是SQL语句跟其他无关. 一:前提知识:PreparedStatement PreperedStat ...

  7. python利用xmlrpc方式对odoo数据表进行增删改查操作

    # -*- encoding: utf-8 -*- import xmlrpclib #导入xmlrpc库,这个库是python的标准库. username ='admin' #用户登录名 pwd = ...

  8. 利用SQLiteOpenHelper创建数据库,进行增删改查操作

    Android中提供SQLiteOpenHelper类,在该类的构造器中,调用Context中的方法创建并打开一个指定名称的数据库对象.继承和扩展SQLiteOpenHelper类主要做的工作就是重写 ...

  9. 利用PHP连接数据库——实现用户数据的增删改查的整体操作实例

    main页面(主页面) <table width="100%" border="1" cellpadding="0" cellspac ...

随机推荐

  1. Linux基础入门(新版)(实验五至实验八)

    实验五 环境变量与文件查找 (环境变量的作用与用法,及几种搜索文件的方法)   一.环境变量   1.变量 (1)常变量与值是一对一的关系 (2)变量的作用域即变量的有效范围(比如一个函数中.一个源文 ...

  2. 在magneto系统中输出tier price的最小值

    2012年6月16日星期六 Asia/Shanghai上午11时39分22秒 有的时候,我们想输出产品的tier price 的最小值!如图: 下面是解决的办法: 1. 在catalog/produc ...

  3. (基础篇)PHP流程控制语句

    不论是PHP还是别的语法,程序总是由若干条语句组成. 从执行方式上看,语句的控制结构分为以下三种: 1.  顺序结构:从第一条语句到最后一条语句完全顺序执行: 2.  选择结构:根据用户输入或语句的中 ...

  4. iOS学习笔记---oc语言第七天

    类的扩展 NSDate是Cocoa中用于处理日期和时间的基础类,封装了某一给定的时刻,具体的日期 时间和时区 使用+date方法获取当前日期和时间 NSDate *date = [NSDate dat ...

  5. 162. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  6. 隐藏select最右侧的下拉三角图标的css样式

    -webkit-appearance:none; -moz-appearance:none; appearance:none;

  7. UVa 814邮件传输代理的交互

    好吧,终于知道原来string有这么多用法,map可以通过vector来映射多个数 #include <iostream> #include <string> #include ...

  8. Quailty and Binary Operation

    Quailty and Binary Operation 题意 分别给\(N,M(N,M \le 50000)\)两个数组\(A\)和\(B\),满足\(0 \le A_i,B_i \le 50000 ...

  9. DDL、DML、

     SQL语言的分类 SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 1. 数据查询语言DQL数据查询语言DQL基本结构是由SELECT子句,F ...

  10. Integer相加产生的类型转换问题

    做项目时犯二没有搞清楚优先级的问题从而暴露出一个Integer相加而产生的类型转换的问题 Integer a; Integer b; Integer c; c=  a+b==null?a:b; jav ...