首页 > 其他 > 详细

控制台程序操作Dynamic数据

时间:2021-09-13 19:00:39      阅读:24      评论:0      收藏:0      [点我收藏+]

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; //需要引用的包 using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query; using System.ServiceModel; using System.ServiceModel.Description; namespace IndexDB_Access { public class Program { //连接服务,进行CRUD操作 Uri orgServiceUri = new Uri("http://10.20.100.150:7777/Index/XRMServices/2011/Organization.svc"); ClientCredentials clientCredentials = new ClientCredentials(); public void Run() { clientCredentials.Windows.ClientCredential = new NetworkCredential("lanhai", "P@ssw0rd", "LANHAI"); try { using (OrganizationServiceProxy sev = new OrganizationServiceProxy(orgServiceUri, null, clientCredentials, null)) { Console.WriteLine("连接成功!域名为:LANHAI"); Console.WriteLine("登录名为:lanhai"); Console.WriteLine($"组织服务:{orgServiceUri.AbsoluteUri}"); sev.EnableProxyTypes(); //添加一条实体信息 Console.WriteLine("1.添加"); Console.WriteLine("2.查询"); Console.WriteLine("3.修改"); Console.WriteLine("4.删除"); Console.Write("输入需要进行的操作:"); int num=Convert.ToInt32(Console.ReadLine()); bool f = true; do { switch (num) { case 1://添加 Console.Write("请输入需要添加的学生名称:"); string new_stu_name = Console.ReadLine(); Add(new_stu_name); break; case 2://查询 //显示学生名称 Sel(); break; case 3://修改 Console.Write("请输入需要添加的学生名称:"); string upd_stu_name = Console.ReadLine(); Update(upd_stu_name); break; case 4://删除 Console.Write("请输入需要删除的学生名称:"); string stu_name = Console.ReadLine(); Del(stu_name); break; default: Console.WriteLine("已退出"); break; } Console.Write("是否继续操作!y/n:"); string tf=Console.ReadLine(); if (tf == "y") { f = true; } else { f = false; } } while (f); //执行相应的操作 Console.ReadLine(); } } catch (Exception) { Console.WriteLine("请检查服务器是否开启!"); } } //主进程 static void Main(string[] args) { Program app = new Program(); app.Run(); } //添加 public void Add(string stu_name) { using (OrganizationServiceProxy sev = new OrganizationServiceProxy(orgServiceUri, null, clientCredentials, null)) { Entity e = new Entity("new_name"); e.Attributes["new_name"] = stu_name; sev.Create(e); Console.WriteLine("已添加!"); } } //删 public void Del(string stu_name) { using (OrganizationServiceProxy sev = new OrganizationServiceProxy(orgServiceUri, null, clientCredentials, null)) { QueryExpression q = new QueryExpression("new_student"); q.ColumnSet = new ColumnSet(true); q.Criteria.AddCondition("new_name", ConditionOperator.Equal, stu_name); EntityCollection ee = sev.RetrieveMultiple(q); if (ee.Entities.Count==0) { Console.WriteLine("请输入正确的名称!"); } else { for (int i = 0; i < ee.Entities.Count-1; i++) { sev.Delete("new_student",ee.Entities[i].Id); } Console.WriteLine("删除成功!"); } } } //改 public void Update(string stu_name) { using (OrganizationServiceProxy sev = new OrganizationServiceProxy(orgServiceUri, null, clientCredentials, null)) { QueryExpression q = new QueryExpression("new_student"); q.ColumnSet = new ColumnSet(true); q.Criteria.AddCondition("new_name", ConditionOperator.Equal, stu_name); EntityCollection ee = sev.RetrieveMultiple(q); Entity e = new Entity("new_student"); if (ee.Entities.Count == 0) { Console.WriteLine("请输入正确的名称!"); } else { for (int i = 0; i < ee.Entities.Count - 1; i++) { e.Id = ee.Entities[i].Id; e.Attributes["new_name"] = "修改名称!"; sev.Update(e); } Console.WriteLine("修改成功!"); } } } //查 public void Sel() { using (OrganizationServiceProxy sev = new OrganizationServiceProxy(orgServiceUri, null, clientCredentials, null)) { QueryExpression q = new QueryExpression("new_student"); q.ColumnSet = new ColumnSet(true); EntityCollection ee = sev.RetrieveMultiple(q); Console.WriteLine("学生名称"); for (int i = 0; i < ee.Entities.Count - 1; i++) { Console.WriteLine(ee.Entities[i].Attributes["new_name"]); ; } } } } }

控制台程序操作Dynamic数据

原文:https://www.cnblogs.com/LanHai12/p/15257994.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!