控制台程序实现利用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 ...
随机推荐
- Unity3D ShaderLab Use Properties
在上一篇,学会了怎么使用ShaderLab 语法:Properties,这一次,我们将会使用属性值点亮材质球. 1:Delete>sampler2D _MainTex; 2:1行位置Add> ...
- WP8.1 双击两次返回键退出程序
在实现Windows Phone上实现点按两次返回键退出程序, 一种方法是使用Coding4Fun提供的ToastPrompt, 使用方法如下: 1. 安装引用, 打开Package Manager ...
- IOS7新特性 edgesForExtendedLayout
edgesForExtendedLayout是一个类型为UIExtendedEdge的属性,指定边缘要延伸的方向. 因为iOS7鼓励全屏布局,它的默认值很自然地是UIRectEdgeAll,四周边缘均 ...
- 尽量多的以 const/enum/inline 替代 #define
前言 在面向过程语言,如 C 语言中,#define 非常常见,也确实好用,值得提倡.但在如今面向对象的语言,如 C++ 语言中,#define 就要尽量少用了. 为何在 C++ 中就要少用了呢? 这 ...
- could not write file C:\DOCUME~1\ADMIN
今天碰到一个错误,android模拟器无法启动,错误提示如下: NAND: could not write file C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\\Andro ...
- 最最实用的30个Linux命令!
本文中将介绍一些实用又常用的Linux或Unix命令,这些是Linux系统管理员们平常使用的命令.本文不是什么完整列表,而是简要地列出了需要时派得上用场的命令,下面开始逐一介绍如何使用这些命令并附有示 ...
- ZOJ 3878--解题报告
题目相关: 3878相关链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5526 Edward 打字员习惯于QWERTY键 ...
- ZSDRM001-发货清单
REPORT ZSDRM001 LINE-SIZE 225 LINE-COUNT 65 NO STANDARD PAGE HEADING.*------------------------------ ...
- ZOJ 1045 HangOver
原题链接 题目大意:叠扑克牌,给出伸出长度,问最多需要几张扑克牌. 解法:循环累加.退出循环后向上取整输出. 参考代码: #include<iostream> using namespac ...
- POJ 1321 棋盘问题 --- DFS
POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...