csharp SAP连接类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp SAP连接类相关的知识,希望对你有一定的参考价值。

using SapROTWr;
using System;
using SAPFEWSELib;

namespace MyApp.Services.SAP
{
    public enum SAPServer
    {
        Quality,
        Production
    }

    public static class SAPConnection
    {
        private const string Quality = "Quality Server";
        private const string Production = "Production Server";

        public static CSapROTWrapper SAPROTWrapper { get; set; }
        public static object SAPGuiRot { get; set; }
        public static GuiConnection GUIConnection { get; set; }
        public static GuiSession GUISession { get; set; }
        public static GuiApplication GUIApp { get; set; }
        public static GuiFrameWindow GUIFrameWindow { get; set; }

        static SAPConnection()
        {
            SAPROTWrapper = new CSapROTWrapper();
            SAPGuiRot = SAPROTWrapper.GetROTEntry("SAPGUI");
        }

        public static void Connect(SAPServer connection)
        {
            CreateGUIApp();
            switch(connection)
            {
                case SAPServer.Quality:
                    CreateGUIConnection(Quality);
                    break;
                case SAPServer.Production:
                    CreateGUIConnection(Production);
                    break;
            }

            GUISession = GUIConnection?.Children.ElementAt(0) as GuiSession;
            GUIFrameWindow = GUISession.FindById("wnd[0]") as GuiFrameWindow;
        }

        private static void CreateGUIApp()
        {
            if (SAPGuiRot == null)
            {
                GUIApp = (GuiApplication)Activator.CreateInstance(Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1"));
            }
            else
            {
                GUIApp =
                    SAPGuiRot.GetType()
                        .InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null,
                            SAPGuiRot, null) as GuiApplication;
            }
        }

        private static void CreateGUIConnection(string connectionName)
        {
            if (GUIApp?.Connections.Count == 0)
            {
                GUIConnection = GUIApp.OpenConnection(connectionName);
            }
            else
            {
                GUIConnection = GUIApp?.Children.ElementAt(0) as GuiConnection;
            }
        }
    }
}

以上是关于csharp SAP连接类的主要内容,如果未能解决你的问题,请参考以下文章

csharp TQUERY SAP

csharp llenar grid zh SAP B1

csharp Agreagar elementos是tabla SAP B1

csharp Filtrar combo del sistema en SAP(no probado)

C#如何通过RFC连接SAP系统

调用JCO连接SAP的问题