• 控制台程序操作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"]); ;
                    }
                    
                }
    
            }
        }
    }
  • 相关阅读:
    jquery 获取当前元素的索引值
    JQuery中根据属性或属性值获得元素(6种情况获取方法)
    jquery如何获取某一个兄弟节点
    JAVA学习<六>
    JAVA学习<四>
    JAVA学习<三>
    iOS定位到崩溃代码行数
    Swift3.0基础语法学习<五>
    Swift3.0基础语法学习<四>
    Swift3.0基础语法学习<三>
  • 原文地址:https://www.cnblogs.com/LanHai12/p/15257994.html
Copyright © 2020-2023  润新知