Skip to content

Queries

The following section describes how to control and extract SAP Queries with ERPConnect.

Note

SAP Queries refer to queries that can be created by the SAP transactions SQ02 and SQ01. If you want to use a BW Query as a data source, see BW Cube/Queries.

About SAP Queries

SAP Queries are used to access data sets, see SAP Help - Working with Queries for more information.

The input of a Query is defined by the SelectParameters collection. Every input parameter can contain an arbitrary number of values. The values are represented by ranges. In addition, a query object contains a Fields collection that in turn contains the output fields.

The figure below shows the object hierarchy that is provided by the Queries classes in ERPConnect.
SAP Query Object Model

Call Queries

The following sample shows how to fetch data from an SAP Query using ERPConnect.

  1. Connect to the SAP system using R3Connection.
  2. Create a query object using CreateQuery. Each SAP query is defined by three descriptors:
    • the user workspace (local or global)
    • the user group
    • the name of the query
  3. Define the parameters of the query. Every parameter has a distinctly technical name that can be looked up in the SAP.
    Example: To create a selection criteria, access the parameter's range collection via the indexer and add a new range using Add. An individual range is defined by three descriptors:
    • a sign (include or exclude)
    • an operator (e.g. equals for parity)
    • a low or high value (LowValue and HighValue or only LowValue when using parity).
  4. Execute the query using Execute.
  5. The query returns a table of the DataTable type. Access the result table using Result attribute.

Tip

If you don't know the technical name of a Query parameter, you can look it up in your SAP-System. Use the transaction SQ01 or SQ02 to search a Query. Open the description of the Query and scroll to the section Selections. All parameter properties including the name are listed.

Fetch Data from SAP Query D3
private void btnFetchQueryData_Click(object sender, System.EventArgs e)
    {
      using (R3Connection con = new R3Connection("SAPServer", 00, "SAPUser", "Password", "EN", "800"))
        { 
           con.Open();

           // Create Query object Query q; 
           try
           {
               Query q = con.CreateQuery(WorkSpace.GlobalArea, "BT", "D3");

               // Add a criteria (in this case the airline) 
               q.SelectionParameters["CARRID"].Ranges.Add(
                   Sign.Include, RangeOption.Equals, inputAirline.Text);
               // Add a second criteria (in this case the date) 
               q.SelectionParameters["FLDATE"].Ranges.Add(
                   Sign.Include, RangeOption.Between, inputStartDate.Text, inputEndDate.Text);

               // Run the Query 
               q.Execute();
               // Bind result to datagrid 
               this.dataGridView1.DataSource = q.Result;
           }
           catch (Exception e1)
           {
            MessageBox.Show(e1.Message);
            return;
           }
        }
    }


Last update: June 25, 2026