Asp.net reads SAP data (Full Process in RFC format)

Source: Internet
Author: User

Platform: vs2005, ecc6, orcale Database

1. First, use se37 to create a READ function,

 

 

 

 

 

 

 

 

2. Add three COM components: InterOP. sapfunctionsocx. dll, InterOP. saplogonctrl. dll, and InterOP. saptablefactoryctrl. dll.

 

The following code is used to reprint other websites,

 

The function module is called in the web form basically the same as the form. The only thing worth noting is: "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. ". that is to say, some acrtivex controls or COM components must run in a single thread unit (STA: single thread apartment); otherwise, a "bad variant type" exception will be thrown. Solution: open a new thread, set the running mode of the thread to STA, and then call the COM component or ActiveX control under the thread.

In our case, if we do not run in Sta mode, we can connect to the SAP system, but an "Bad variant type" exception will be thrown when the function module is called. Therefore, the code that calls the function module is executed in the new thread. The procedure is as follows:

1. Add references to the InterOP. sapfunctionsocx. dll, InterOP. saplogonctrl. dll, and InterOP. saptablefactoryctrl. dllcom components.

2. Open a new thread, set the running mode of the thread to STA, and run the methods used to log on to the SAP system and call the function module under the thread! The Code is as follows:

Protected void button#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 );
}
}
}

At the same time, a friend may encounter a Chinese display as #. In this case, you need to set the system's environment variable sap_codepage = 8400 and install the corresponding sap client patch.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.