asp.net 讀取SAP資料(rfc形式全過程)

來源:互聯網
上載者:User

平台:vs2005,ecc6 ,orcale 資料庫

1.首先用se37,建立一個讀取函數,

 

 

 

 

 

 

 

 

2.先添加三個com組件Interop.SAPFunctionsOCX.dll,Interop.SAPLogonCtrl.dll,Interop.SAPTableFactoryCtrl.dll

 

下面的代碼我是轉載其他網站, 

 

以web形式對function module進行調用與form形式基本一樣,唯一值得注意的地方就是:"An ActiveX control must be run in an STA apartment. Because the attribute STAThread is applied to the Main method of a WinForm class by default with a WinForms Project, the main thread of your app will run in STA mode.".也就是說有些AcrtiveX控制項或者Com組件必須運行在單一執行緒 Apartment下(STA:Single  Thread  Apartment ),否則的話會拋出“Bad variant type”異常。解決方案為:新開一個線程,並將該線程的運行模式設定為STA,然後再改線程下對Com組件或者ActiveX控制項進行調用。

      對應到我們的案例中,如果不使用STA模式運行,我們可以串連到SAP系統,但調用Function Module的時候會拋出“Bad variant type”異常。所以要講調用Function Module的代碼在新開的線程中執行。具體步驟如下:

      一,添加對Interop.SAPFunctionsOCX.dll以及Interop.SAPLogonCtrl.dll和Interop.SAPTableFactoryCtrl.dllcom組件的引用。

      二,新開一個線程,並將該線程的運行模式設定為STA. 並將登入SAP系統以及調用Function module的方法運行在該線程下!代碼如下:

     protected void Button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread s = new System.Threading.Thread(new System.Threading.ThreadStart(test)); //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();                                                                                        //Wait until thread run OK.
        GridView1.DataSource = dt;
        GridView1.DataBind();
        msg.Text = "Get Data from 'ENQUEUE_READ' OK!";
    }

    private void test()
    {
        SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass();
        login.ApplicationServer = "";
        login.Client = "";
        login.Language = "EN";
        login.User = username.Text;
        login.Password = Psw.Text;
        login.SystemNumber = 00;
        SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection();

        if (conn.Logon(0, true))
        {
            SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();
            func.Connection = conn;
            SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("ENQUEUE_READ"); 
            SAPFunctionsOCX.IParameter gclient = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("GCLIENT");
            gclient.Value = "301";
            SAPFunctionsOCX.IParameter GUNAME = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("GUNAME");
            GUNAME.Value = "";
            SAPFunctionsOCX.IParameter LOCAL = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("LOCAL");
            LOCAL.Value = "0";
            ifunc.Call();
            SAPTableFactoryCtrl.Tables tables = (SAPTableFactoryCtrl.Tables)ifunc.Tables;
            SAPTableFactoryCtrl.Table ENQ = (SAPTableFactoryCtrl.Table)tables.get_Item("ENQ");
            int n = ENQ.RowCount;
            dt = GetTable();
            for (int i = 1; i <= n; i++)
            {
                DataRow dr = dt.NewRow();
                dr["GNAME"] = ENQ.get_Cell(i, "GNAME").ToString();
                dr["GUNAME"] = ENQ.get_Cell(i, "GUNAME").ToString();
                dr["GARG"] = ENQ.get_Cell(i, "GARG").ToString();
                dr["GOBJ"] = ENQ.get_Cell(i, "GOBJ").ToString();
                dr["GTDATE"] = ENQ.get_Cell(i, "GTDATE").ToString();
                dt.Rows.Add(dr);
            }
        }
    }

同時有朋友會遇到中文顯示為#,這個時候就需要設定系統的環境變數sap_codepage=8400,同時安裝sap 對應的用戶端補丁

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.