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:
- MODIFY ZNCOTEST FROM TABLE IT_MYNCOTEST[].
IF SY-SUBRC = 0.
R_SUBRC = 'OKKK'.
ENDIF.
ENDIF.
- 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: