阅读 87

控制台程序操作Dynamic数据

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"]); ; } } } } }

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

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐