.NET用NCO连接SAP RFC---写数据到SAP

1.环境:

    a. win7+64位操作系统 

    b. VS2012  

    c. nco3.0(64bit 下载网址:http://www.dllbang.com/dll/sapnco_dll ),

d. Microsoft Visual C++ 2005 Service Pack 1 ,用于运行nco3.0的dll文件
    下载网址:http://www.microsoft.com/en-us/download/details.aspx?id=14431 

2.目的:将数据通过RFC插入SAP自建表。

3.步骤:

第一步:进入SAP界面:使用T- CODE:SE11   打开建表界面:建立表ZNCOTEST


图片1:


第二步:针对自建表ZNCOTEST创建RFC。

使用T- CODE:SE37   打开建RFC界面:命名为:ZNCOTEST_RFC

图片2:



RFC如下表:在TABLE参数TABLE里设置IT_MYNCOTEST 类型是刚才自建的表ZCHANNEL_MESSAG。

图片3:



在EXPORT里设置出参R_SUBRC.

图片4:


在SOURCE CODE里写代码:

图片5:



IF IT_MYNCOTEST[] IS NOT INITIAL.
  1.   MODIFY ZNCOTEST FROM TABLE IT_MYNCOTEST[].
      IF SY-SUBRC 0.
        R_SUBRC 'OKKK'.
      ENDIF.
    ENDIF.

  1. ENDFUNCTION.

然后,注意要把RFC属性设为允许远程访问:

图片6:



到此在SAP这边的工作已经就绪。


第三步:剩下的都在VS2010下完成:

打开VS2010新建一个WINDOWS窗体应用程序:WindowsFormsApplication2


拖动一个按钮控件到主窗体:


在解决方案资源管理器,引用里引用NCO3.0的sapnco.dll和sapnco_utils

 

右击项目名称WindowsFormsApplication2,点击“属性”,打开属性面板:将目标框架里默认的.NET Framework 4 Client Profile 改为:.NET Framework 4。(很重要)


在解决方案资源管理器里打开配置文件APP.CONFIG,配置如下:

代码:

<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="SAP.Middleware.Connector">
<sectionGroup name="ClientSettings">
<section name="DestinationConfiguration" type="SAP.Middleware.Connector.RfcDestinationConfiguration,sapnco"/>
</sectionGroup>
</sectionGroup>
</configSections>


<SAP.Middleware.Connector>
<ClientSettings>
<DestinationConfiguration>
<destinations>
<add NAME="DEV" USER="username" PASSWD="password" CLIENT="500"
                         LANG="ZH"  ASHOST="198.16.0.66" SYSNR="00"
                         MAX_POOL_SIZE="10" IDLE_TIMEOUT="10"/>
</destinations>
</DestinationConfiguration>
</ClientSettings>
</SAP.Middleware.Connector>


<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>


双击Button1按钮进入代码编辑器:

手工引用:using SAP.Middleware.Connector;


为按钮添加单击事件如下:

private void button1_Click(object sender, EventArgs e)
        {
           
            RfcDestination SapRfcDestination = RfcDestinationManager.GetDestination("DEV");
            RfcRepository SapRfcRepository = SapRfcDestination.Repository;
            // Create and invhuoke function moduleZCHANNEL_RFC_MESSAGE  
            IRfcFunction myfun = SapRfcRepository.CreateFunction("ZNCOTEST_RFC");
            // Set some input values for the structure.  
            IRfcStructure import = null;
            IRfcTable table = myfun.GetTable("IT_MYNCOTEST");
            int i = 3;
            while (i < 6)
            {
                int j = i + 1;
                string s=j+"S";
                import = SapRfcRepository.GetStructureMetadata("ZNCOTEST").CreateStructure();
                import.SetValue("ID", s);
                import.SetValue("NAME", "Name" + s);
                import.SetValue("REMARK", "Remark" + s);
                table.Insert(import);
                i++;
            }
            myfun.Invoke(SapRfcDestination);
            string RETURNStr = myfun.GetString("R_SUBRC");
            MessageBox.Show(RETURNStr);  
        }


在sap的se16中查看 运行结果:

图片7:




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值