1.分析传参结构
SAP 传参格式对应 .NET 参数格式
SAP 参数 | .NET 参数 | 参数类型 |
---|---|---|
import(导入)——关联类型为数据元素 | Param | 单个变量参数 |
import(导出)——关联类型为结构体 | Struct | 结构体 |
table | Table | 表 |
下面是 SAP 对应参数类型:
2.web.config 配置
配置文件需要客户端名和连接地址和账户密码
<appSettings>
<add key="SAPINSName" value="OND"/> //客户端名字
</appSettings>
<SAP.Middleware.Connector>
<ClientSettings>
<DestinationConfiguration>
<destinations>
<add NAME="OND" USER="" PASSWD="******" CLIENT="300" LANG="ZH" ASHOST="10.10.xx.xx" SYSNR="00" MAX_POOL_SIZE="10" IDLE_TIMEOUT="10"/>
</destinations>
</DestinationConfiguration>
</ClientSettings>
</SAP.Middleware.Connector>
3.调用示例:
调用 SAP 接口类型为输入输出表的接口:
var SAP = ConfigHelper.AppSettings("SAPINSName");
Test.SapDBBase.SAPBaseDAL sd = new Test.SapDBBase.SAPBaseDAL(SAP); //基本设置
DataTable tb = new DataTable();
tb.Columns.Add("WERKS");
tb.Columns.Add("MATNR");
DataRow rowsap = tb.NewRow();
rowsap["WERKS"] = WERK;
rowsap["MATNR"] = PN;
tb.Rows.Add(rowsap);
try
{
//调用 SAP RFC 接口,传参是表结构,返回表数据和结构体
DataTable returntd = sd.WriteDataTable("ZFM_EXPORT", "IT_IN", tb, "IT_OUT");
}
catch (Exception e)
{
Type = "E";
Msg = e.Message;
}
4.调用方法部分代码
- 调用准备
using System;
using System.Collections;
using System.Data;
using System.Linq;
using ONET.ServerCommLib;
using SAP.Middleware.Connector;
public SAPBaseDAL(string SapConfigNodeName = "")
{
if (!string.IsNullOrEmpty(SapConfigNodeName))
{
SapRFCBase.SapConfigNodeName = SapConfigNodeName;
}
}
try
{
RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
RfcRepository repository = rfcDestination.Repository;
IRfcFunction rfcFunction = repository.CreateFunction(functionName);
...//不同入参类型
rfcFunction.Invoke(rfcDestination);
...//不同出参类型
}
catch (Exception ex)
{
this.writeExceptionLog(functionName, ex);
}
- 出入参构造
//入参结构体
rfcFunction = this.GetIRfcStructeFromDataTable(rfcFunction, writeSapStructName, writeStructTable);
//入参表
rfcFunction = this.GetIRfcTableFromDataTable(rfcFunction, writeSapTableName, writeTable);
rfcFunction = writeDataSet.Tables.Cast<DataTable>().Aggregate(rfcFunction, (IRfcFunction current, DataTable table) => this.GetIRfcTableFromDataTable(current, table.TableName, table));//多表
//入参单个变量
if (htParam != null && htParam.Count > 0)
{
foreach (object obj in htParam.Keys)
{
string text = (string)obj;
rfcFunction.SetValue(text, htParam[text].ToString());
}
}
//出参结构
方式1:
if (!string.IsNullOrEmpty(returnStructName))
{
IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
return new SAPRetStruct
{
Type = structure.GetString("TYPE"),
Msg = structure.GetString("MESSAGE")
};
}
return default(SAPRetStruct);
方式2:
//ref Hashtable hstb 返回hashtable
IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
if (hstb != null && hstb.Count > 0)
{
ArrayList arrayList = new ArrayList(hstb.Keys);
for (int i = 0; i < arrayList.Count; i++)
{
hstb[arrayList[i]] = structure.GetString(arrayList[i].ToString());
}
}
//出参单个变量参数
Hashtable hashtable = new Hashtable();
if (outParam != null && outParam.Count > 0)
{
foreach (object obj2 in outParam.Keys)
{
string text2 = (string)obj2;
hashtable.Add(text2, rfcFunction.GetString(text2));
}
if (hashtable.Count > 0)
{
outParam.Clear();
foreach (object obj3 in hashtable.Keys)
{
string text2 = (string)obj3;
outParam.Add(text2, hashtable[text2]);
}
}
}
//出参表
IRfcTable table = rfcFunction.GetTable(returnTableName);//出参表名
dtOut = this.GetDataTableFromRFCTable(table); //返回的表
- 实例1:入参:变量+表 出参:变量+结构体
public SAPRetStruct WriteParamAndTableToSapReturnParamAndRetStruct(string functionName, Hashtable htParam, string writeSapTableName, DataTable writeTable, ref Hashtable outParam, string returnStructName)
{
try
{
RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
RfcRepository repository = rfcDestination.Repository;
IRfcFunction rfcFunction = repository.CreateFunction(functionName);
rfcFunction = this.GetIRfcTableFromDataTable(rfcFunction, writeSapTableName, writeTable);
if (htParam != null && htParam.Count > 0)
{
foreach (object obj in htParam.Keys)
{
string text = (string)obj;
rfcFunction.SetValue(text, htParam[text].ToString());
}
}
rfcFunction.Invoke(rfcDestination);
Hashtable hashtable = new Hashtable();
if (outParam != null && outParam.Count > 0)
{
foreach (object obj2 in outParam.Keys)
{
string text2 = (string)obj2;
hashtable.Add(text2, rfcFunction.GetString(text2));
}
if (hashtable.Count > 0)
{
outParam.Clear();
foreach (object obj3 in hashtable.Keys)
{
string text2 = (string)obj3;
outParam.Add(text2, hashtable[text2]);
}
}
}
IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
return new SAPRetStruct
{
Type = structure.GetString("TYPE"),
Msg = structure.GetString("MESSAGE")
};
}
catch (Exception ex)
{
this.writeExceptionLog(functionName, ex);
}
return default(SAPRetStruct);
}
- 实例2:入参:多个表 出参:变量+结构体
public SAPRetStruct WriteTablesToSapReturnParamAndRetStruct(string functionName, DataSet writeDataSet, ref Hashtable outParam, string returnStructName)
{
try
{
RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
RfcRepository repository = rfcDestination.Repository;
IRfcFunction rfcFunction = repository.CreateFunction(functionName);
rfcFunction = writeDataSet.Tables.Cast<DataTable>().Aggregate(rfcFunction, (IRfcFunction current, DataTable table) => this.GetIRfcTableFromDataTable(current, table.TableName, table));
rfcFunction.Invoke(rfcDestination);
Hashtable hashtable = new Hashtable();
if (outParam != null && outParam.Count > 0)
{
foreach (object obj in outParam.Keys)
{
string text = (string)obj;
hashtable.Add(text, rfcFunction.GetString(text));
}
if (hashtable.Count > 0)
{
outParam.Clear();
foreach (object obj2 in hashtable.Keys)
{
string text = (string)obj2;
outParam.Add(text, hashtable[text]);
}
}
}
IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
return new SAPRetStruct
{
Type = structure.GetString("TYPE"),
Msg = structure.GetString("MESSAGE")
};
}
catch (Exception ex)
{
this.writeExceptionLog(functionName, ex);
}
return default(SAPRetStruct);
}
- 实例3:入参:变量 出参:表
public DataTable GetTable(string functionName, Hashtable htParam, string returnTableName)
{
try
{
RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
RfcRepository repository = rfcDestination.Repository;
IRfcFunction rfcFunction = repository.CreateFunction(functionName);
if (htParam != null && htParam.Count > 0)
{
foreach (object obj in htParam.Keys)
{
string text = (string)obj;
rfcFunction.SetValue(text, htParam[text].ToString());
}
}
rfcFunction.Invoke(rfcDestination);
IRfcTable table = rfcFunction.GetTable(returnTableName);
return this.GetDataTableFromRFCTable(table);
}
catch (Exception ex)
{
this.writeExceptionLog(functionName, ex);
}
return null;
}