The following sample shows how to fetch data from a BW Query using ERPConnect.
About the Sample Query #
This example uses the BW query ZSIMPLEQUERY which is based on the cube 0D_DECU.
The following screenshot shows the query in the designer.
Note: To allow external access to a BW Query the checkbox Allow external access has to be set to active in the preferences for the query.
The dimensions Material and Sold-to party as well as the key figures Billed Quantity and Costs are drawn into a DataGrid, see screenshot below.
The dimension has a variable called MAT01 that allows a limitation to the material number.
Executing Queries #
- Open a client connection to the R/3 system using the R3Connection class.
- Use the CreateCube function to create a BWCube object. Its name is made up of the cube name and query name.
- The cube object offers a collection for all contained dimensions (Dimensions) and key figures (Measures).
If the attribute SelectForFlatMDX is set to true, the component is added to the query output.
Add the dimensions Material and Sold-to party and the key figures Billed Quantity and Costs to the query output. - To fill the variables with values, address them via the variables collection. There are range tables behind each variable.
- Execute the BW query with Execute. The output is a table of the DataTable type.
Note: The denomination of the key figures via the query generation in the designer does not conform to the original technical name. That means key figures are addressed by the ordinal number, not the name.
Sample Code #
private void Go_Click(object sender, System.EventArgs e)
{
using (R3Connection con = new R3Connection("SAPServer", 00, "SAPUser", "Password", "EN", "800"))
{
BWCube query = con.CreateBWCube("0D_DECU/ZSIMPLEQUERY");
query.Dimensions["0D_MATERIAL"].SelectForFlatMDX = true;
query.Dimensions["0D_SOLD_TO"].SelectForFlatMDX = true;
query.Measures[0].SelectForFlatMDX = true;
query.Measures[1].SelectForFlatMDX = true;
query.Variables["MAT01"].SingleRange.LowValue = this.txtMatNr.Text;
this.dataGrid1.DataSource = query.Execute();
}
}