jgugugiug 发表于 2015-9-19 08:34:23

Sap.net 连接使用实例

private void SAPConnect()
      {
            /* this routine creates a proxy object, if one doesn't already
             * exist and creates the SAP connection if needed. It's called
             * before each SAP RFC call just in case */

            // create one proxy for all methods
            if (null == proxy)
            {
                proxy = new SAPProxy1();
            }
            if (false == g_IsConnected)
            {
                proxy.Connection =new SAP.Connector.SAPConnection(this.destination1);   
                try {proxy.Connection.Open();}
                catch (Exception ex)
                {MessageBox.Show("Invalid SAP connection, please fix" + ex.ToString());}
            }                        
      } //SAPConnect()
private void SAPAsyncSearch()
      {
    /* this routine calls RFC_CUSTOMER_GET using .Net asynchronous
   * method invocation. When the function is completed asynchronously in SAP,
   * the function "myfunction" is called. */
            SAPConnect();
            myAsyncState = null;
            myCallback = new System.AsyncCallback(myFunction);
            asyncresult = null;
            try
            {
                asyncresult = proxy.BeginRfc_Customer_Get(g_custNo, g_custName, ref brfcknA1Table1, myCallback, myAsyncState);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error returned in Async search\n" + ex.ToString(), "SAP Async Search problem");
                return;
            }
      }

      private void myFunction(IAsyncResult ar)
      {
            /* this function gets called when rfc function is done executing
             * asynchronously. It reconnects the main form thread to the results */
            MessageBox.Show("async call is returned from SAP", "Asynchronous update status");
            try
            {
                proxy.EndRfc_Customer_Get(asyncresult, ref brfcknA1Table1);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception occurred in async callback \n" + ex.ToString());
                return;
            }
            finally
            {
                proxy.Connection.Close();
                g_IsConnected = false;
            }
            
            if (0 < brfcknA1Table1.Count)
            {
                SetShowAllMenu(this.mainMenu1);
            }
                        
      } // myfunction

      private void SAPTableToXML()
      {
            /* this routine saves the SAP table from RFC_CUSTOMER_GET
             * into an XML file. It uses the save dialog control to ask the
             * user what file to save to. It creates the xml file by serializing the SAP Table (BRFCKna1Table) */
            
            // Use save dialog box to get the file to save into
            saveFileDialog1.ShowDialog();
            string file = saveFileDialog1.FileName;
            
            // serialize to xml
            XmlSerializer xs = new XmlSerializer(typeof(BRFCKNA1Table));
            System.Text.Encoding unicode = new System.Text.UnicodeEncoding();
            XmlTextWriter xtw = new XmlTextWriter(file, unicode);
            xs.Serialize(xtw, brfcknA1Table1);
            xtw.Close();
      
      } //SAPTableToXML

      private void ShowCustSelect()
      {
            /* this form gets the RFC_CUSTOMER_GET input
             * criteria. The criteria are Name and/or customer No. We need these values
             * before calling the RFC.
             */

            // show a dialog box to enter customer search selections
            frmCustomerSelection fc = new frmCustomerSelection(g_custNo, g_custName);
            fc.ShowDialog(this);

            // save the results to my variables for later use
            if (null != fc.Cust)
            {
                g_custName = fc.Cust.CustName;
                g_custNo = fc.Cust.CustNo;
                // we have selections now but not data so update menu status
                SetNoDataMenu();
                this.statusBar1.Text = "Customer selection: Name: " + g_custName + " number: " + g_custNo;
            }
      } //showCustSelec
页: [1]
查看完整版本: Sap.net 连接使用实例