控制台程序实现利用CRM组织服务和SqlConnection对数据库中数据的增删改查操作
一、首先新建一个控制台程序。命名为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对数据库中数据的增删改查操作的更多相关文章
- 使用JDBC分别利用Statement和PreparedStatement来对MySQL数据库进行简单的增删改查以及SQL注入的原理
一.MySQL数据库的下载及安装 https://www.mysql.com/ 点击DOWNLOADS,拉到页面底部,找到MySQL Community(GPL)Downloads,点击 选择下图中的 ...
- AD 域服务简介(三)- Java 对 AD 域用户的增删改查操作
博客地址:http://www.moonxy.com 关于AD 域服务器搭建及其使用,请参阅:AD 域服务简介(一) - 基于 LDAP 的 AD 域服务器搭建及其使用 Java 获取 AD 域用户, ...
- PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码
PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...
- Go微服务框架go-kratos实战03:使用 gorm 实现增删改查操作
一.简介 在上一篇文章 go-kratos实战02 中,详细介绍了用 kratos 编写项目代码的步骤.这篇就在上篇基础上,再结合 Go 数据库操作库 gorm 一步一步来实现一个简单的增删改查操作. ...
- mysql详解常用命令操作,利用SQL语句创建数据表—增删改查
关系型数据库的核心内容是 关系 即 二维表 MYSQL的启动和连接show variables; [所有的变量] 1服务端启动 查看服务状态 sudo /etc/init.d/mysql status ...
- JavaWeb程序利用Servlet的对SQLserver增删改查操作
声明:学了几天终于将增删改查的操作掌握了,也发现了一些问题,所以总结一下. 重点:操作数据库主要用的是SQL语句跟其他无关. 一:前提知识:PreparedStatement PreperedStat ...
- python利用xmlrpc方式对odoo数据表进行增删改查操作
# -*- encoding: utf-8 -*- import xmlrpclib #导入xmlrpc库,这个库是python的标准库. username ='admin' #用户登录名 pwd = ...
- 利用SQLiteOpenHelper创建数据库,进行增删改查操作
Android中提供SQLiteOpenHelper类,在该类的构造器中,调用Context中的方法创建并打开一个指定名称的数据库对象.继承和扩展SQLiteOpenHelper类主要做的工作就是重写 ...
- 利用PHP连接数据库——实现用户数据的增删改查的整体操作实例
main页面(主页面) <table width="100%" border="1" cellpadding="0" cellspac ...
随机推荐
- nginx+c+cgi开发
http://blog.csdn.net/marising/article/details/3932938 1.Nginx 1.1.安装 Nginx 的中文维基 http://wiki.codemon ...
- clone 深拷贝 浅拷贝
1. 定义:知道一个对象,但不知道类,想要得到该对象相同的一个副本,在修改该对象的属性时,副本属性不修改,clone的是对象的属性 2. 意义:当一个对象里很多属性,想要得到一个相同的对象,还有set ...
- Java ArrayList的使用方法
首先ArrayList的一个简单实例: package chapter11; import java.util.ArrayList; public class TestArrayList { publ ...
- 主机控制器接口(HCI)
主机控制器接口(HCI)提供了一种访问蓝牙硬件能力的通用接口. HCI固件通过访问基带命令.链路管理器命令.硬件状态寄存器.控制寄存器以及事件寄存器实现对蓝牙硬件的HCI命令. HCI是通过包的方式来 ...
- myeclipse 清理项目缓存的几大步骤
http://blog.csdn.net/moneyshi/article/details/49247169 相信大家被项目缓存折腾过吧,这里罗列几条清除项目缓存的方法 1.项目清理: 选择菜单栏的P ...
- sgu259 Printed PR 贪心
link:http://acm.sgu.ru/problem.php?contest=0&problem=259 思路就是贪心. 首先要读懂题目,输入的方式,把样例读懂. 第一,打印的总时间一 ...
- sgu548 Dragons and Princesses 贪心+优先队列
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=548 题目意思: 有一个骑士,要经过n个房间,开始在第一个房间,每个房间里面有龙或者 ...
- JS数组随机排序
var arr=[1,2,3,4,5]; arr.sort(function(a,b){ var v=Math.random()>0.5?1:-1; console.log(a,b,v); re ...
- javascript闭包,arguments和prototype
prototype javascript中一切皆对象,并且对象的属性和方法可以任意添加,例如: var obj=function(){}; obj.name="jack"; 但是下 ...
- __LINE__ __DATE__ __FILE__ __TIME__ 等宏定义解释
Names the predefined ANSI C and Microsoft C++ implementation macros. The compiler recognizes predefi ...