ASP.Net MVC3连接SAP实践

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using SAP.Middleware.Connector;

using System.Data;

namespace SAPMVC.Controllers

{

    public class HomeController : Controller

    {

        string MATNR = string.Empty;

        public ActionResult Index()

        {

            ViewBag.Message = "Welcome to ASP.NET MVC!";

            nco();

            //nco2();



            return View();

        }



        public void nco2()

        {

            RfcConfigParameters rfcPar = new RfcConfigParameters();

            //rfcPar.Add(RfcConfigParameters.Name, "CON");

            //rfcPar.Add(RfcConfigParameters.AppServerHost, "192.168.1.3");

            //rfcPar.Add(RfcConfigParameters.Client, "800");

            //rfcPar.Add(RfcConfigParameters.User, "UserID");

            //rfcPar.Add(RfcConfigParameters.Password, "Password");

            //rfcPar.Add(RfcConfigParameters.SystemNumber, "02");

            //rfcPar.Add(RfcConfigParameters.Language, "EN");



            rfcPar.Add(RfcConfigParameters.Name, "CON");

            rfcPar.Add(RfcConfigParameters.AppServerHost, "192.168.1.3");   //SAP主机IP

            rfcPar.Add(RfcConfigParameters.SystemNumber, "00");  //SAP实例

            rfcPar.Add(RfcConfigParameters.User, "MENGXIN");  //用户名

            rfcPar.Add(RfcConfigParameters.Password, "5239898");  //密码

            rfcPar.Add(RfcConfigParameters.Client, "888");  // Client

            //rfcPar.Add(RfcConfigParameters.Language, "ZH");  //登陆语言

            //rfcPar.Add(RfcConfigParameters.PoolSize, "5");

            //rfcPar.Add(RfcConfigParameters.MaxPoolSize, "10");

            //rfcPar.Add(RfcConfigParameters.IdleTimeout, "60");



            RfcDestination dest = RfcDestinationManager.GetDestination(rfcPar);

            RfcRepository rfcrep = dest.Repository;

            IRfcFunction myfun = null;

            myfun = rfcrep.CreateFunction("SAP里面的函数名称");

            myfun.SetValue("VTYPE", "0");//SAP里面的传入参数

            myfun.Invoke(dest);

            IRfcTable IrfTable = myfun.GetTable("IT_ZMYTB2");

            //提前实例化一个空的表结构出来

            DataTable dt = new DataTable();

            dt.Columns.Add("USERID");

            dt.Columns.Add("USERPWD");

            dt.Columns.Add("USERADDRESS");

            //循环把IRfcTable里面的数据放入Table里面,因为类型不同,不可直接使用。

            for (int i = 0; i < IrfTable.Count; i++)

            {

                IrfTable.CurrentIndex = i;

                DataRow dr = dt.NewRow();

                dr["USERID"] = IrfTable.GetString("USERID");

                dr["USERPWD"] = IrfTable.GetString("USERPWD");

                dr["USERADDRESS"] = IrfTable.GetString("USERADDRESS");

                dt.Rows.Add(dr);

            }

        }



        public void nco()

        {

            IDestinationConfiguration ID = new MyBackendConfig();

            RfcDestinationManager.RegisterDestinationConfiguration(ID);

            RfcDestination prd = RfcDestinationManager.GetDestination("PRD_000");

            RfcDestinationManager.UnregisterDestinationConfiguration(ID);

            nco(prd);

        }

        public void nco(RfcDestination prd)

        {

            RfcRepository repo = prd.Repository;

            IRfcFunction companyBapi = repo.CreateFunction("ZRFC_MARA_INFO");   //调用函数名

            companyBapi.SetValue("MATNR", MATNR);   //设置Import的参数

            companyBapi.Invoke(prd);   //执行函数

            IRfcTable table = companyBapi.GetTable("IT_MARA");  //获取相应的品号内表

            string MAKTX = companyBapi.GetValue("MAKTX").ToString();  //获取品名

            DataTable dt = new DataTable();  //新建表格

            dt.Columns.Add("品号");  //表格添加一列

            for (int i = 0; i < table.RowCount; i++)

            {

                table.CurrentIndex = i;  //当前内表的索引行

                DataRow dr = dt.NewRow();

                dr[0] = table.GetString("MATNR");  //获取表格的某行某列的值

                dt.Rows.Add(dr);  //填充该表格的值

            }

            if (MATNR == "")

            {

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                    //this.comboBox1.Items.Add(dt.Rows[i][0].ToString());   //填充下拉框

                }

            }

            //this.label1.Text = MAKTX;   //显示品名

            prd = null;

            repo = null;

        }



        public ActionResult About()

        {

            return View();

        }



        //登陆SAP前的准备工作

        public class MyBackendConfig : IDestinationConfiguration

        {

            public RfcConfigParameters GetParameters(String destinationName)

            {

                if ("PRD_000".Equals(destinationName))

                {

                    RfcConfigParameters parms = new RfcConfigParameters();

                    //parms.Add(RfcConfigParameters.Name, "PRD_000");

                    parms.Add(RfcConfigParameters.AppServerHost, "192.168.1.3");   //SAP主机IP

                    parms.Add(RfcConfigParameters.SystemNumber, "00");  //SAP实例

                    parms.Add(RfcConfigParameters.User, "MENGXIN");  //用户名

                    parms.Add(RfcConfigParameters.Password, "5239898");  //密码

                    parms.Add(RfcConfigParameters.Client, "888");  // Client

                    parms.Add(RfcConfigParameters.Language, "ZH");  //登陆语言

                    parms.Add(RfcConfigParameters.PoolSize, "5");

                    parms.Add(RfcConfigParameters.MaxPoolSize, "10");

                    parms.Add(RfcConfigParameters.IdleTimeout, "60");

                    return parms;

                }

                else return null;

            }

            public bool ChangeEventsSupported()

            {

                return false;

            }

            public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;

        }

    }

}


你可能感兴趣的:(asp.net)