ABAP写好RFC(Remote Function Call), C#调用, 取得数据放入非SAP环境下的数据库做SAP外围系统.
RFC:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
FUNCTION ZRFC_RESB_DATA.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(ZRSNUM) LIKE RESB-RSNUM DEFAULT SPACE
*" TABLES
*" RESB_LIST STRUCTURE ZRFC_RESB_LIST
*" EKPO_LIST STRUCTURE ZRFC_EKPO_LIST
*" PRODPLANT_RANGE STRUCTURE BAPI_ORDER_PRODPLANT_RANGE
*"----------------------------------------------------------------------
IF ZRSNUM IS NOT INITIAL.
SELECT
RSNUM
RSPOS
MATNR
AUFNR
BDMNG
MEINS
VORNR
AUFPL
PLNFL
SORTF
BAUGR
POTX1
POTX2
MATKL
DUMPS
ENMNG
BDTER
KZEAR
INTO TABLE RESB_LIST
FROM RESB
WHERE RSNUM = ZRSNUM AND BDMNG > 0 AND XLOEK <> 'X'.
IF RESB_LIST[] IS NOT INITIAL.
LOOP AT RESB_LIST.
SHIFT RESB_LIST-AUFNR LEFT DELETING LEADING '0'.
SHIFT RESB_LIST-MATNR LEFT DELETING LEADING '0'.
MODIFY RESB_LIST.
ENDLOOP.
ENDIF.
ENDIF.
ENDFUNCTION.
C# winform:
C#开发环境, 也是SAP客户端(未安装SAP客户端的PC, 找不到下列三个DLL)
using SAPFunctionsOCX;
using SAPLogonCtrl;
using SAPTableFactoryCtrl;
button事件 (web下需要新线程来执行SAP连接)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void button1_Click(object sender, EventArgs e)
{
GETTBL();
label1.Text = "...completed!";
}
连接SAP并执行RFC
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void GETTBL()
{
SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass();
login.ApplicationServer = "ip address";
login.Client = "clientCode";
login.Language = "ZH";
login.User = "userName";
login.Password = "password";
login.SystemNumber = 00;
SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection();
if (conn.Logon(0, true)) //login successful
{
SAPFunctionsOCX.SAPFunctionsClass func1 = new SAPFunctionsOCX.SAPFunctionsClass();
func1.Connection = conn;
SAPFunctionsOCX.IFunction ifunc1 = (SAPFunctionsOCX.IFunction)func1.Add("ZRFC_RESB_DATA");
string T_aufnr;
string o_aufnr;
DataTable dt;
dt = GetDataSet("SELECT b.rsnum,a.aufnr FROM EBom as a inner join aufk as b on a.aufnr = b.aufnr WHERE a.state IS NULL", "EBOM");
if (dt.Rows.Count >= 1)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
T_aufnr = dt.Rows[i].ItemArray[0].ToString();
o_aufnr = dt.Rows[i].ItemArray[1].ToString();
SAPFunctionsOCX.IParameter zaufnr = (SAPFunctionsOCX.IParameter)ifunc1.get_Exports("ZRSNUM");
zaufnr.Value = T_aufnr;
ifunc1.Call();
SAPTableFactoryCtrl.Tables bcENQs = (SAPTableFactoryCtrl.Tables)ifunc1.Tables;
SAPTableFactoryCtrl.Table cyRESB = (SAPTableFactoryCtrl.Table)bcENQs.get_Item("RESB_LIST");
if (cyRESB.RowCount >= 1) //EBOM
{
for (int w = 1; w <= cyRESB.RowCount; w++)
{
if (aAUFNR == cyRESB.get_Cell(w, "aufnr").ToString())
{
DMARK1 = 0;
}
else
{
DMARK1 = 1;
}
insertRESB(
Convert.ToInt32(cyRESB.get_Cell(w, "rsnum")),
Convert.ToInt32(cyRESB.get_Cell(w, "rspos")),
cyRESB.get_Cell(w, "matnr").ToString(),
cyRESB.get_Cell(w, "aufnr").ToString(),
Convert.ToDecimal(cyRESB.get_Cell(w, "bdmng")),
cyRESB.get_Cell(w, "meins").ToString(),
cyRESB.get_Cell(w, "vornr").ToString(),
Convert.ToInt32(cyRESB.get_Cell(w, "aufpl")),
cyRESB.get_Cell(w, "plnfl").ToString(),
cyRESB.get_Cell(w, "sortf").ToString(),
cyRESB.get_Cell(w, "baugr").ToString(),
cyRESB.get_Cell(w, "potx1").ToString(),
cyRESB.get_Cell(w, "potx2").ToString(),
cyRESB.get_Cell(w, "matkl").ToString(),
cyRESB.get_Cell(w, "dumps").ToString(),
Convert.ToDecimal(cyRESB.get_Cell(w, "enmng")),
Convert.ToDateTime(cyRESB.get_Cell(w, "bdter")),
cyRESB.get_Cell(w, "KZEAR").ToString(),
DMARK1);
aAUFNR = cyRESB.get_Cell(w, "aufnr").ToString();
}
ExecSQL("UPDATE EBOM SET state = 'EBOM' where aufnr ='" + o_aufnr + "'");
}
else
{
ExecSQL("UPDATE EBOM SET state = 'ebom_err' where aufnr ='" + o_aufnr + "'");
}
cyRESB.FreeTable();
}
}
}
}
用到的三个函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public System.Data.DataTable GetDataSet(string sQueryString, string TableName)
{
string strCon = ConfigurationManager.ConnectionStrings["ConnectString"].ToString();
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlDataAdapter dbAdapter = new SqlDataAdapter(sQueryString, con);
DataTable dt = new DataTable();
dbAdapter.Fill(dt);
con.Close();
return dt;
}
public Boolean ExecSQL(string sQueryString)
{
string strCon = ConfigurationManager.ConnectionStrings["ConnectString"].ToString();
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand dbCommand = new SqlCommand(sQueryString, con);
try
{
dbCommand.ExecuteNonQuery();
con.Close();
}
catch
{
con.Close();
return false;
}
return true;
}
public void insertRESB(Int32 rsnum, Int32 rspos, string matnr, string aufnr, decimal bdmng, string meins,
string vornr, Int32 aufpl, string plnfl, string sortf, string baugr, string potx1, string potx2, string matkl,
string dumps, decimal enmng, DateTime bdter, string KZEAR, Int32 DMARK1)
{
string strCon = ConfigurationManager.ConnectionStrings["ConnectString"].ToString();
SqlConnection conn = new SqlConnection(strCon);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_RESB";
cmd.Parameters.Add(new SqlParameter("@rsnum", rsnum));
cmd.Parameters.Add(new SqlParameter("@rspos", rspos));
cmd.Parameters.Add(new SqlParameter("@matnr", matnr));
cmd.Parameters.Add(new SqlParameter("@aufnr", aufnr));
cmd.Parameters.Add(new SqlParameter("@bdmng", bdmng));
cmd.Parameters.Add(new SqlParameter("@meins", meins));
cmd.Parameters.Add(new SqlParameter("@vornr", vornr));
cmd.Parameters.Add(new SqlParameter("@aufpl", aufpl));
cmd.Parameters.Add(new SqlParameter("@plnfl", plnfl));
cmd.Parameters.Add(new SqlParameter("@sortf", sortf));
cmd.Parameters.Add(new SqlParameter("@baugr", baugr));
cmd.Parameters.Add(new SqlParameter("@potx1", potx1));
cmd.Parameters.Add(new SqlParameter("@potx2", potx2));
cmd.Parameters.Add(new SqlParameter("@matkl", matkl));
cmd.Parameters.Add(new SqlParameter("@dumps", dumps));
cmd.Parameters.Add(new SqlParameter("@enmng", enmng));
cmd.Parameters.Add(new SqlParameter("@bdter", bdter));
cmd.Parameters.Add(new SqlParameter("@KZEAR", KZEAR));
cmd.Parameters.Add(new SqlParameter("@DMARK1", DMARK1));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
一个存储过程
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =======================================================
-- Author: <LIN>
-- Create date: <>
-- Description: <Windows Service insert ...>
-- ========================================================
ALTER proc [dbo].[sp_RESB](
@rsnum int
,@rspos int
,@matnr varchar(18)
,@aufnr varchar(12)
,@bdmng decimal(13,3)
,@meins varchar(3)
,@vornr varchar(4)
,@aufpl int
,@plnfl varchar(6)
,@sortf varchar(10)
,@baugr varchar(18)
,@potx1 varchar(40)
,@potx2 varchar(40)
,@matkl varchar(9)
,@dumps nchar(1)
,@enmng decimal(13,3)
,@bdter datetime
,@KZEAR nchar(1)
,@DMARK1 INT
)
AS
Begin
IF ISNULL(@DMARK1,0)= 1
BEGIN
DELETE FROM RESB WHERE aufnr=@aufnr
END
INSERT INTO RESB(rsnum,rspos,matnr,aufnr,bdmng,meins,vornr,aufpl,plnfl,sortf,baugr,potx1,potx2,matkl,dumps,enmng,bdter,KZEAR)
VALUES(@rsnum,@rspos,@matnr,@aufnr,@bdmng,@meins,@vornr,@aufpl,@plnfl,@sortf,@baugr,@potx1,@potx2,@matkl,@dumps,@enmng,@bdter,@KZEAR)
End