.net连接SAP的几种方式

 

.net连接SAP的几种方式

一、 SAP .net connector

这是SAP专为.net连接开发的一个工具,简单方便,但是只支持VS2003.当然想在VS2003以上的版本使用可以选择在VS2003上包装下再用。

使用方法:

1.首先安装SAP .net connector,一路next即可

2.打开VS2003,新建一个项目,打开server explorer,你会发现多了一个SAP的server图标(如果没有,点击服务器浏览器的刷新按钮即可),添加一个服务器,设置方式选择”自定义登录方式”,然后配置用户名,密码,服务器地址,客户端等

3.配置完毕后会出现一个SAP服务器,展开,在选择“Functions”节点,添加一个filter,过滤下,以免该用户名下的RFC太多。添加完毕后会出现一个RFC

4.在项目上新建一个SAP Connectot proxy,并设置其属性如下图

5.将RFC拖到该proxy上

6.开始编码

C# Code

SAP.Connector.Destination destination = new SAP.Connector.Destination();

destination.Username = "PULLSYSTEM"; // SAP username

destination.Password = "npullpwd"; // SAP username's password

destination.AppServerHost = "hksapecc"; // SAP application server's ip number or id

estination.Client = 800; // Client number

destination.SystemNumber = 0; // application server system number

SAP.Connector.SAPConnection sapConnection = new SAP.Connector.SAPConnection(destination);

SAPProxy1 proxy = new SAPProxy1();

proxy.Connection = sapConnection;

proxy.Z_RFC_PO_INF(strPO,strPoItem,out strPRPO,out strI2PRPO,

out strPRItemPO,out strBuyerCodePO,out strBuyerNamePO,

out E_INSMK,out strCostCenterPO,out strSlocPO,out strVendorPO,

out strPartNOPO,out strUnitPO,out strQtyPO,out strPricePO,

out E_PSTYP,out strGLPO,out strPlantPO,out E_ZPMAT);

proxy.Connection.Close();

二、 是用SAP客户端,调用com组件连接SAP

使用方法:

1. 引用一下组件:

2. 开始编码

C# Code

private void trytoconnectSAP()

{

try

{

SAPLogonCtrl.SAPLogonControlClass logon = new SAPLogonCtrl.SAPLogonControlClass();

logon.ApplicationServer = "10.10.209.164"; //SAP system's IP

logon.Client = "800"; //SAP system'client

logon.Language = "EN";

logon.User = "PULLSYSTEM"; //Username

logon.Password = "npullpwd"; //Password

logon.SystemNumber = 00; //System number

SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)logon.NewConnection();

if (conn.Logon(0, true))

{

SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();

func.Connection = conn;

SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("Z_RFC_PO_INF"); //Call Function module 'ENQUEUE_READ'

SAPFunctionsOCX.IParameter gclient = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("L_EBELN"); //Get the import paremeter

gclient.Value = strPONO; //"4500111574"; //Set value for import paremeter

SAPFunctionsOCX.IParameter GUNAME = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("L_EBELP");

GUNAME.Value = strPOITEM; //10

ifunc.Call();

SAPFunctionsOCX.IParameter Plant = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("E_WERKS");

string plant = Plant._Value.ToString();

SAPFunctionsOCX.IParameter Sloc = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("E_LGORT");

string sloc = Sloc._Value.ToString();

SAPFunctionsOCX.IParameter Qty = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("E_MENGE");

string qty = Qty._Value.ToString();

conn.Logoff();

strPO = plant + "/" + sloc + "/" + qty;

}

}

catch (Exception ex)

{

strPO = ex.Message;

}

}

注意,在asp.net下使用,必须用线程的技术进行调用,否则出错

C# Code

System.Threading.Thread s = new System.Threading.Thread(new System.Threading.ThreadStart(trytoconnectSAP)); //Create a new thread and set the method test() run in this thread

s.SetApartmentState(System.Threading.ApartmentState.STA); //Set the run mode 'STA'

s.Start(); //Start the thread

s.Join();

三、 使用Data Provider for mySAP Business Suite进行连接

这个组件时在Biztalk上使用的,编码比较简单,但是需要用SAP的权限或者让人在SAP中传入两个RFC,这两个RFC的作用是收集和整理RFC,具体可以参看“自述文件”

以下是代码片段

C# Code

public bool TryConnectSAPByProvider()

{

try

{

string strConn = System.Configuration.ConfigurationSettings.AppSettings["SAPConnstring"].ToString();

SAPConnection con = new SAPConnection(strConn);//SAP服务器连接参数设置,

con.Open();

SAPCommand cmd = new SAPCommand(con);

cmd.CommandText = "select * from Z_RFC_PO_INF where L_EBELN='9800031143' and L_EBELP='00010'";//执行远程RFC BAPI_CUSTOMER_GETLIST,执行RFC的参数传递过程参考下边的EXEC 语句的语法

//以下为RFC调用参数赋值并指定Input、Output类型

//SAPParameter param = new SAPParameter("@param", ParameterDirection.InputOutput);

//DataTable dt = new DataTable();

//dt.Columns.Add("SIGN");

//dt.Columns.Add("OPTION");

//dt.Columns.Add("LOW");

//dt.Columns.Add("HIGH");

//DataRow row = dt.NewRow();

//row["LOW"] = 1;

//row["HIGH"] = 1000;

//dt.Rows.Add(row);

//param.Value = dt;

//cmd.Parameters.Add(param);//执行结果放在SAPDataReade中

SAPDataReader dr = cmd.ExecuteReader(); //retrieving returned datareaders

return true;

}

catch (Exception ex)

{

return false;

}

附件下载:SAP .net connect

你可能感兴趣的:(.net)