Net 调用SAP RFC接口来读取数据

下面把我调试的全过程都记录一下,以后有人遇到相同问题就可以参考了。

1.【关键前提】:

   (1) 你安装了VS2003 (注意:一定要VS2003,原因在下面)

   (2)安装SAP.Net Connector 2.0 (这东东目前只支持.net framework 1.X, 即IDE 7.5版本,所以只能用VS2003)

   (3) 有Java runtime environment (后面导入SAP的Function时有用)

   (4) 安装SAP Logon

2.【开始编程】

   (1)首先是在SAP里面创建RFC

   (2) VS里面编程

先以Winform程序为例吧

a.新建winform项目,因为装了SAP.Net Connector,所以可以添加对SAP.Connector.dll和SAP.Connector .Rfc.dll的引用。在项目中新建类,你会发现多了一个SAP Connector Proxy的类,新建一个,就是下面图中的SAPProxy1.sapwsdl文件啦。

b.在Form1.cs中添加控件

(比较简陋,凑合看了)

c.在SAP Test的Button事件里面加入以下代码

private void button1_Click(object sender, System.EventArgs e)

  {

   SAP.Connector.SAPLogonDestination myDest;

   SAP.Connector.SAPConnection myConn;

   SAPProxy1 myProx;

   BRFCKNA1Table xMara;

   System.Data.DataTable dMara;

   try

   {

    myDest = new SAP.Connector.SAPLogonDestination();

    myDest.DestinationName = "这里输入SAP服务器上的系统名称";

    myDest.Client = 这里输入SAP服务器的客户端号;

    myDest.Username = "这里输入SAP服务器访问的用户名";

    myDest.Password = "这里输入SAP服务器访问的密码";

    myConn = new SAP.Connector.SAPConnection(myDest);

    myConn.Open();

    xMara = new BRFCKNA1Table();//注意,这个BRFCKNA1Table可不是随便写的哈,看到下面就明白了

    myProx = new SAPProxy1();

    myProx.Connection = myConn;

    myProx.Rfc_Customer_Get("", this.textBox1.Text, ref xMara);

    // 注意,这个Rfc_Customer_Get就是你要call的RFC的function名称

    dMara = new System.Data.DataTable();

    dMara = xMara.ToADODataTable();

    dataGrid1.DataSource = dMara;

    dataGrid1.Refresh();

    myConn.Close();

   }

   catch(Exception ex)

   {

    textBox1.Text = ex.ToString();

   }

  }

d.从SAP服务器上导入RFC Function

不要小看这一步哦

 【支线任务1】在C:/WINDOWS找到saplogon.ini,打开后把SysName和SrvPort下面的内容清空,不清空的话他会引导你去找SAP 服务器上的Messaging server,总是告诉你无法打开或者无法找到sapmsg.ini的文件,非常麻烦。

[MSSysName]

Item1=

[MSSrvPort]

Item1=

(如果机器上没有Java runtime environment 就会一直停留在Loading的画面=.= )

  e. 启动项目,在textbox里面输入检索关键字,就可以看到这样的画面,调用成功^0^

 

上面是以Winform为例,下面简单说一下Web App和Web Service,还是有点差别的哟

【Web App】

Web app的不同之处在于

(1)在default.aspx中加入button,datagrid以及textbox后,在后台程序中加入如下代码:

private void Button1_Click(object sender, System.EventArgs e)

  {

   // Declare parameters here

   SAPProxy1 proxy = new SAPProxy1();

   SAPRFCIF.BRFCKNA1Table brfcknA1Table1 = new BRFCKNA1Table();

   try

   {

    proxy.Connection = SAP.Connector.SAPLoginProvider.GetSAPConnection(this);

    // Call methods here

    proxy.Rfc_Customer_Get("", this.TextBox1.Text, ref brfcknA1Table1);

    // Now update Data Bindings. On WinForms this will be automatic, on  // WebForms call the following line

    this.DataBind();

   }

   catch(Exception ex)

   {

    // If SAPLoginProvider.GetSAPConnection(this) cannot get a connection, // we might get an error.

    // Normally this can be ignored as it will automatically will force a // relogon.

   }

  }

(2)需要新建一个SAPLogin1.aspx,这个也是装了SAP.net Connector以后,在新增项目的时候可以看到的一个aspx模板,然后在SAPLogin1.aspx.cs中添加如下代码

string systemNumber = "00";//这个也是要看SAP服务器设置的

    private void Login_Click(object sender, System.EventArgs e)

    {

      // Bind data back

      this.destination1.Username = this.user.Text;

      this.destination1.Password = this.password.Text;

  //设置sap服务器 start

   this.destination1.AppServerHost="这个自己填哈";

   this.destination1.SystemNumber= short.Parse(systemNumber);

  ;

  //设置sap服务器 end

      try

      {

        this.destination1.Client = short.Parse(this.client.Text);

      }

      catch(Exception)

      {

        this.destination1.Client = 0;

      }

      if (this.language.Text.Length > 0)

      {

        this.destination1.Language = this.language.Text;

      }

      try

      {

        SAP.Connector.SAPLoginProvider.OpenSAPConnection(this, destination1.ConnectionString, this.persist.Checked);

      }

      catch (System.Exception exception)

      {

        this.message.Text = exception.ToString();

      } 

    }

【Web Service】

(1) 在Service1.asmx中添加SAP的Destination控件

 

右键,打开代码

 

(2) 在Service1.asmx.cs中加入以下代码

 private void InitializeComponent()

  {

   //以下是定義Destination的內容       

   this.components = new System.ComponentModel.Container();

   this.destination1 = new SAP.Connector.Destination(this.components);

   //  

   //   destination1

   //  

   this.destination1.AppServerHost = "XXXXXXX";

   this.destination1.Client = ((short)(XXX));

   this.destination1.Language = "EN";

   this.destination1.Password = "XXXX";

   this.destination1.SystemNumber = ((short)(00));

   this.destination1.Username = "XXXX";

   //----------------------------------------

  }

 

[WebMethod]

  public DataSet Exploding_BOM(string Language)

  {

   try

   { 

    SAPProxy1 mProxy=new SAPProxy1();           //實例一個SAPProxy

    mProxy.Connection=SAP.Connector.Connection.GetConnectionFromPool(this.destination1.ConnectionString);       //鏈接

    RFCHOSTSTable mStpoxtable = new RFCHOSTSTable();           //實例一個輸出資料的Table          

    DateTime mNow = DateTime.Now;            

    string str_now = mNow.ToString();           //把取到的當前日期轉為字串型

    mProxy.Rfc_Get_Local_Destinations(Language, ref mStpoxtable);

    mProxy.Connection.Close();

    mProxy.Connection=null;

    DataSet mReturn = new DataSet(); //實例一個DataSet﹐存放RFC回傳的Table

    mReturn.Tables.Add(mStpoxtable.ToADODataTable ());

    return mReturn;

   }

   catch (Exception   ex)

   {

    return DataError(ex);

   }

  }

  public DataSet DataError(Exception ex)

  {

   DataSet errDS = new DataSet( "Errors");

   DataTable errTable = errDS.Tables.Add( "Error ");

   errTable.Columns.Add( "Message ");

   errTable.Rows.Add(new Object[] {ex.Message});

   return errDS;

  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值