心心失意 发表于 2015-9-18 08:25:56

Delphi 连接SAP

  本人对DELPHI开发工具熟悉,所以测试连接的时候用DELHI来做例子,其它JAVA. DotNet估计原理应该是一样的,先说下要做的例子;
  1. 如何将ACTIVEX控件注册到DELPHI, 以便利用SAP这些控件;
2. 利用SAP自动的FUNCTION:BAPI_PO_CREATE ,测试如何通过DELPHI去调用这个函数,创建采购订单
  例子很简单,但是通过这个例子可以知道SAP于其它开发工具交互的一种方式(RFC:远程调用),整个例子不需要在SAP做什么配置(原以为需要在DBCO中建立
连接,看来那个跟此没有关系),开始做下,按此步骤:
1。安装sap客户端,安装SAPGui和SAP SDK (如果你是SAP使用者,估计这部都做过了,没有安装在DELPHI中找不到相应的控件的)
2. Delphi中安装ActiveX部件
   2.1 SAPLogonControl, SAPBapiControl 安装
      component --> ActiveX import   (imported components: TSAPLogonControl, TSAPBapiControl)
  http://hiphotos.baidu.com/wangnengzhi/pic/item/66d29502e3518eb0d43f7c19.jpg

   2.2 SAP remote Function call control安装
      project --> type library import (imported components: TSAPFunctions, TFunction, TParameter, TExports, TImports, TStructure)
  http://hiphotos.baidu.com/wangnengzhi/pic/item/3d0d1b0fe8e58ff27bcbe1e4.jpg

   系统会自动建立一个文件:TSAPFunctionsOCX_TLB.pas
   做完这两步骤后,可以在DELPHI中看到控件如下:
  http://hiphotos.baidu.com/wangnengzhi/pic/item/e686eb276be7462f8a82a1e1.jpg

  3.开始写DELPHI代码,测试连接
    3.1 新建一个DELPHI PROJECT,将SAPLogonControl1控件和SAPFunctions1控件拖到界面上;
3.2 再放两个按钮:一个是建立SAP连接,一个是调用SAP FUNCTION
   Connection :variant;(全局变量)
3.3 建立SAP连接:
       Connection                  := SAPLogoncontrol1.newConnection;
    Connection.User               := Ansiuppercase('wangnz');
    Connection.System             := 'IDS';
    Connection.Client             := '800';
    Connection.ApplicationServer := '192.168.5.112';
    Connection.SystemNumber       := '00';
    Connection.Password         := '321';
    //Connection.Language         := 'DE' ; //注意这个语言的,填DE还登录不上
    SAPLogonControl1.Enabled      := false;
  if Connection.LogOn(0,true) = True then
    begin
    ShowMessage('Logon O.K.');
    btn_CreatePO.Enabled:= true;
    SapBapiControl1.Connection:=Connection;
    sapFunctions1.Connection := Connection;
    end
    else
    begin
    ShowMessage('Error on logon :-(((');
    end;
    3.4 调用SAP FUNCTION:BAPI_PO_CREATE
   var MLDText : String;
    Funct,Header,POItems,Schedules,ItemsRow,
   SchedulesRow: Variant;
   begin
    (* define function *)
  Funct := sapFunctions1.add('BAPI_PO_CREATE');
  (*** define tables, use structures of the dictionary ***)
  (* table for the purcaseorder header *)
    Header := funct.exports('PO_HEADER');
  (* table of the purcaseorder items *)
    POItems := funct.tables.item('PO_ITEMS');
  (* table of the schedules *)
    Schedules := funct.tables.item('PO_ITEM_SCHEDULES');
  (*** filling the PO_Header-table ***)
  (* purcasing document type *)
    Header.Value := 'NB' ;
  (* purcasing document category *)
    Header.Value := 'F' ;
  Header.Value := '1000' ; //公司代码
    (* purcasing organisation 采购组织 *)
    Header.Value := '1000' ;
  (* purcasing group *)
    Header.Value := '026' ;
  (* forget the leading zeroes!!!                     *)
    Header.Value := '111';   //供应商
  (*** filling the PO_Items-table ***)
  (* add new row to the table *)
    ItemsRow := POItems.rows.add;
  (* item number of purcasing document *)
    ItemsRow.Value:='00010';
  (* material-number, on numeric values don't forget *)
    (* the leading zeros !!!                            *)
    ItemsRow.Value:='100-210';
  (* storage location *)
    ItemsRow.Value:='0001';
  (* plant *)
    ItemsRow.Value:='1000';
  (* netprice in purcasing document, *)
    (* in document currency            *)
    ItemsRow.Value:='10000';
  (*** filling the PO_Items_Schedules-table ***)
  (* add new row to the table *)
    SchedulesRow := Schedules.rows.add;
  (* item number of purcasing document *)
    SchedulesRow.Value:='00010';
  (* category of delivery date *)
    SchedulesRow.Value:='1';
  (* item delivery date *)
    SchedulesRow.Value:='20000523';
  (* scheduled quantity *)
    SchedulesRow.Value:='10';
  (*** call function ***)
  if not funct.call then
    (* on error show message *)
    showMessage(funct.exception)
    else
    begin
    (* show number of the purcaseorder *)
    MLDText:= funct.imports('PURCHASEORDER');
    MessageDlg('purcaseorder '+MLDText+' created.',MTInformation,,0);
    end;
   end;
  如此就将SAP连接起来,具体的实现SAP的功能都在SAP SE37中写功能而已;
页: [1]
查看完整版本: Delphi 连接SAP