欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.Net MVC3连接SAP实践

程序员文章站 2022-05-28 19:34:10
[csharp] using system;  using system.collections.generic;  using system.linq;  using...
[csharp]
using system; 
using system.collections.generic; 
using system.linq; 
using system.web; 
using system.web.m; 
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; 
        } 
    }