使用NCO3.0调用SAP RFC
相比.net connector 2.0,易用性上降低了,但是功能上强大了。1.安装sap .net connector 3.0,在C:\Program Files\SAP\SAP_DotNetConnector3_x86你会得到四个dll文件:sapnco_utils.dll sapnco.dll rscp4n.dll libicudecnumber.dll
2.使用vs2010 我们创建一个项目,添加对上面四个dll文件的引用,实际上只有三个是可以引用成功的,libicudecnumber.dll的引用会失败,注意在这里,你看上去是引用成功的,其实你后面的程序是不能运行的。这个需要你在程序属性中做一些小修改才可以:修改“项目属性”-“应用程序”-“目标框架” ,由“.NET Framework 4 Client Profile”修改为“.NET Framework 4”,最后引用 .NET 4.0 的 System.Web.dll 。只有这样sapnco.dll才可以被正常引用;否则就会提示:“SAP.Middleware.Connector.RfcConfigParameters”的类型初始值设定项引发异常。
3.using SAP.Middleware.Connector;这个不用多说
4.代码:
using SAP.Middleware.Connector;
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)
{
string type = string.Empty;
RfcRepository repo = prd.Repository;
IRfcFunction companyBapi = repo.CreateFunction("ZRFC_MARA_INFO");//指定RFC名称
try
{
companyBapi.SetValue("NUM1", textBox1.Text.Trim());//输入参数复制
companyBapi.SetValue("NUM2", textBox2.Text.Trim());//输入参数复制
companyBapi.Invoke(prd);//开始调用执行
textBox3.Text = companyBapi.GetValue("NUM3").ToString();//获取返回结果
}
catch (RfcAbapException ex)//此Exception专门用于获取用户自定义的异常信息!!!!
{
// companyBapi.Metadata.GetAbapException(ex.Key).Documentation 获取对应的异常的说明文字
MessageBox.Show(companyBapi.Metadata.GetAbapException(ex.Key).Documentation, "SAP RFC返回信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (RfcTypeConversionException ex)//此Exception专门用于获取变量类型转换的异常!!!!
{
MessageBox.Show("您输入的不是数值", "SAP RFC返回信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (RfcAbapRuntimeException ex) //此Exception专门用于获取RFC执行过程中的运行时异常!!!!
{
MessageBox.Show(companyBapi.Metadata.GetAbapException(ex.Key).Documentation, "SAP RFC返回信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (RfcBaseException ex)//此Exception是总Exception类,可以获取所有的异常,如果有多个Catch,则不可以放第一位!!!!
{
MessageBox.Show("其他所有错误", "SAP RFC返回信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
prd = null;
repo = null;
}
public class MyBackendConfig : IDestinationConfiguration
{
public RfcConfigParameters GetParameters(String destinationName)
{
if ("PRD_000".Equals(destinationName))
{
RfcConfigParameters parms = new RfcConfigParameters();
parms.Add(RfcConfigParameters.AppServerHost, "192.168.1.3");
parms.Add(RfcConfigParameters.SystemNumber, "00");
parms.Add(RfcConfigParameters.User, "MENGXIN");
parms.Add(RfcConfigParameters.Password, "5239898");
parms.Add(RfcConfigParameters.Client, "888");
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;
}
private void button1_Click(object sender, EventArgs e)
{
nco();
}
看看其实也很简单,只是没有nco 2.0那样方便而已。至于易用性上,大多数应用情况都是去调RFC,弄懂了这些也足够应付了。
追加:常用的获取、复制等方法
//public IRfcTable GetMaterDatail() {
// RfcRepository repo = RfcDes.Repository;
// IRfcFunction companyBapi = repo.CreateFunction("ZBC_GET_BRAND"); //调用函数名
// IRfcTable tbMatnr = companyBapi.GetTable("IT_MATNR");
// tbMatnr.Append(3);//先想表中添加行,3为行的数量
// for (int i = 0; i < 3; i++) {
// tbMatnr.SetValue(0, "12020077000010");//设置行的值
// }
// //tbMatnr.Insert(2);
// //tbMatnr.Append(2);//Append和Insert都是插入行,区别是插入后默认的行不同
// //tbMatnr.CurrentRow.SetValue(0,"12020077000010");
// //tbMatnr.Insert(3);//插入行后该表默认的CurrentIndex为0,如果不设置CurrentIndex,值一直为0
// ////tbMatnr.CurrentIndex = 0;
// //tbMatnr.CurrentRow.SetValue(0, "12020077000007");
// //tbMatnr.CurrentIndex = 1;
// //tbMatnr.CurrentRow.SetValue(0, "12020077000008");
// //IRfcStructure staaaa = tbMatnr.GetStructure(-1, true);
// //IRfcStructure stMatnr = companyBapi.GetStructure("ZBC_MATNR");
// //IRfcTable tbMatnr = null;
// //IRfcStructure stMatnr = null;
// //stMatnr.SetValue(0, "12020077000007");//物料号,测试时要写入实际值
// //tbMatnr.Insert(stMatnr);
// companyBapi.SetValue("IT_MATNR", tbMatnr); //设置Import的参数
// companyBapi.Invoke(RfcDes); //执行函数
// IRfcTable table = companyBapi.GetTable("ET_AUSP");//获取相应的品号内表
// /*以下部分只是为了把IRfcTable转换成ADOTable,connector2.0时IRfcTable有“.ToAdoTable()”功能,
// 在connector3.0中还未找到此功能”*/
// //DataTable dt = new DataTable();//新建表格
// //dt.Columns.Add("物料编号");//表格添加列
// //dt.Columns.Add("属性编码");
// //dt.Columns.Add("中文描述");
// //dt.Columns.Add("值");
// //for (int i = 0; i < table.RowCount; i++)
// //{
// // table.CurrentIndex = i;//当前内表的索引行
// // DataRow dr = dt.NewRow();
// // dr = table.CurrentRow.GetString(0);//获取表格的某行某列的值 dr=table.GetString(0);
// // dr = table.CurrentRow.GetString(1);
// // dr = table.CurrentRow.GetString(2);
// // dr = table.CurrentRow.GetString(3);
// // dt.Rows.Add(dr);//填充该表格的值
// //}
// return table;
//}
页:
[1]