Execute an SAP Query Using the EXECQUERY Command

The .NET Framework Data Provider for mySAP Business Suite exposes the SAP system as an ADO.NET data source. With the Data Provider for SAP, you can execute pre-defined queries in the SAP system by executing an EXECQUERY statement.

How to Perform a Query by Using the EXECQUERY Command

To execute pre-defined SAP queries using the Data Provider for SAP, perform the following steps:

To perform a query

  1. Include a reference (and a using statement in your code) to Microsoft.Data.SAPClient.

  2. Create a SAPConnection object by using a Data Provider for SAP connection string. For more information about the connection string, see Read about Data Provider types for the SAP Connection String.

  3. Open a connection to the SAP system by invoking Open on the SAPConnection.

  4. Create a SAPCommand object from the SAPConnection.

  5. Specify the EXECQUERY statement in the CommandText property of the SAPCommand. If necessary, you can specify parameters using SAPParameter objects. For more information about how to execute queries defined in an SAP system using an EXECQUERY statement, see Syntax for an EXECQUERY Statement in SAP.

  6. Execute the command to perform the query and obtain the results in a SAPDataReader.

  7. Read the results from the SAPDataReader.

  8. When you are finished using them, close (or dispose) the SAPConnection and the SAPDataReader.

    The Data Provider for SAP also exposes a SAPClientFactory class, which you can use to create SAPConnection, SAPCommand and SAPConnection objects. For more information about the ADO.NET classes extended by the Data Provider for SAP, see Extend ADO.NET Interfaces with the SAP adapter.

Example

The following example writes the results of a query, ZTEST1, to the console.

using System;  
using System.Collections.Generic;  
using System.Text;  
  
using Microsoft.Data.SAPClient;  
  
namespace SapAdoExecQuery  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
           string connstr = "TYPE=A; ASHOST=YourSapHost; SYSNR=00; CLIENT=800; LANG=EN; USER=YourUserName; PASSWD=YourPassword;";  
           using (SAPConnection conn = new SAPConnection(connstr))  
            {  
                conn.Open();  
                using (SAPCommand cmd = conn.CreateCommand())  
                {  
                    cmd.CommandText = "EXECQUERY ZTEST1 @userGRoup='SYSTQV000024',@P1='0000001390',@P2='0000080150'";  
                    cmd.Parameters.Add(new SAPParameter("@connid", 17));                      
                    using (SAPDataReader dr = cmd.ExecuteReader())  
                    {  
                        do  
                        {  
                            int rows = 0;  
                            while (dr.Read())  
                            {  
                                rows++;  
                                StringBuilder b = new StringBuilder();  
                                for (int i = 0; i < dr.FieldCount; i++)  
                                {  
                                    b.Append(dr[i].ToString()+" ");  
                                }  
                                Console.WriteLine("row {0}: {1} ", rows, b.ToString());  
                            }  
                            Console.WriteLine("Number of rows:{0}", rows);  
                        } while (dr.NextResult());  
                    }  
                }  
            }  
        }  
    }  
}  

See Also

Use the .NET Framework Data Provider for mySAP Business Suite
Samples