日期:2014-05-16  浏览次数:20824 次

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 =