如何从SAP返回的ZUBGPMS019表结构中获取指定TBL_ACT的DATA字段?
问题:如何从SAP RFC返回结果中获取TBL_ACT等于"TAR - ENTRY"对应的DATA字段?
现有API实现代码
public HttpResponseMessage RFC_GETEND([FromBody] SapModel model) { RfcDestination dest = RfcDestinationManager.GetDestination(model.Ip); RfcRepository r= dest.Repository; IRfcFunction f = r.CreateFunction("ZUBGP_RFC_019"); f.Invoke(dest); return new HttpResponseMessage() { Content = new StringContent(f[0].GetValue().ToString(), Encoding.UTF8, "text/plain") }; }
SAP返回的表结构
TABLE [STRUCTURE ZUBGPMS019 { FIELD TBL_ACT=VIM - OUT DATA=2019-09-03 }] [STRUCTURE ZUBGPMS019 { FIELD TBL_ACT=TAR - ENTRY FIELD DATA=2021-10-01 }] [STRUCTURE ZUBGPMS019 { FIELD TBL_ACT=TAR - OUT FIELD DATA=2022-12-31 }] [STRUCTURE ZUBGPMS019 { FIELD TBL_ACT=SEC - OUT DATA=2019-06-01 }]
解决方案
你需要先从RFC函数中获取返回的表对象,遍历其中每一行结构,判断TBL_ACT字段值是否匹配目标内容,匹配成功后提取对应的DATA字段值。修改后的代码如下:
public HttpResponseMessage RFC_GETEND([FromBody] SapModel model) { RfcDestination dest = RfcDestinationManager.GetDestination(model.Ip); RfcRepository r = dest.Repository; IRfcFunction f = r.CreateFunction("ZUBGP_RFC_019"); f.Invoke(dest); // 获取返回的表(建议用参数名替代索引,比如f.GetTable("OUT_TABLE"),避免参数顺序变更导致失效) IRfcTable resultTable = f.GetTable(0); string targetData = null; foreach (IRfcStructure row in resultTable) { string tblAct = row.GetString("TBL_ACT"); if (tblAct.Equals("TAR - ENTRY", StringComparison.OrdinalIgnoreCase)) { targetData = row.GetString("DATA"); break; // 找到目标后立即退出循环 } } return new HttpResponseMessage() { Content = new StringContent(targetData ?? "未找到匹配的数据", Encoding.UTF8, "text/plain") }; }
关键说明
- 优先使用参数名获取表对象,而非索引,避免SAP RFC后续修改参数顺序导致代码出错。
- 遍历表结构时,通过
GetString方法读取字段值,用字符串匹配逻辑定位目标行。 - 找到匹配项后立即终止循环,提升执行效率;未找到时返回默认提示文本。
内容的提问来源于stack exchange,提问作者user9618369
相关产品推荐
相关产品推荐

