Skip to Content

Connect Using the SAP HANA .NET Core Interface

0 %
Connect Using the SAP HANA .NET Core Interface

Connect Using the SAP HANA .NET Core Interface

August 16, 2020
Created by
August 16, 2020
Create and debug a .NET Core application that connects to SAP HANA using the SAP HANA client.

You will learn

  • How to install the .NET Core SDK
  • How to create and debug a .NET Core application that queries an SAP HANA database


  • You have completed the first 3 tutorials in this mission.

.NET Core is a free and open source software framework for Microsoft Windows, Linux and Mac operating systems and is the successor to the .NET Framework.

Step 1: Install the .NET Core SDK

The first step is to check if you have the .NET Core SDK installed and what version it is. Enter the following command:

dotnet --version  

If the dotnet command is not recognized, it means that the .Net Core SDK has not been installed. If the SDK is installed, the command returns the currently installed version, such as 3.1.202.

If the .NET Core SDK is not installed, download it from Download .NET and run the installer on Microsoft Windows or Mac.

Note: Select the ‘Build Apps: Download .NET Core SDK’ option.

.NET Core SDK Install

On Linux, follow the instructions for the appropriate Linux version such as openSUSE 15 Package Manager - Install .NET Core.

In order for the shell to recognize that the .Net Core SDK has installed and for any dotnet commands in future steps to be recognized, a new shell window needs to be opened.

For further details on supported versions, see SAP Note 2939501 - SAP HANA Client Supported Platforms for 2.5 and later.

Log on to answer question
Step 2: Create a .NET Core application that queries an SAP HANA database
  1. Create a new console app with the below commands:

    cd %HOMEPATH%/HANAClientsTutorial
    dotnet new console -o dotNET
    export HDBDOTNETCORE=/home/dan/sap/hdbclient/dotnetcore
    cd $HOME/HANAClientsTutorial
    dotnet new console -o dotNET

    On Linux or Mac, modify the HDBDOTNETCORE variable to point to the location of the or libadonetHDB.dylib file.

  2. Open the dotNET.csproj file:

    cd dotNET
    notepad dotNET.csproj
    cd dotNET
    pico dotNET.csproj

    Add the following below the PropertyGroup section to indicate where to load the SAP HANA Client .NET Core driver from. Modify the HintPath section with the information about where the dll is located on your machine.

      <Reference Include="Sap.Data.Hana.Core.v2.1">
      <Reference Include="Sap.Data.Hana.Core.v2.1">
    dotNET.csproj code

    Once the dotNet.csproj file has been updated, save and close the file.

    The SAP HANA Client interface for .NET Core is compatible with version 2.1, and 3.x releases of .NET Core.

  3. Open an editor to edit the file Program.cs.

    notepad Program.cs
    pico Program.cs
  4. Replace the entire contents of Program.cs with the code below:

    using System;
    using Sap.Data.Hana;
    namespace dotNETQuery
    class Program
        static void Main(string[] args)
                // User1UserKey retrieved from hdbuserstore contains server:port, UID and PWD
                // encrypt must be true when connecting to HANA Cloud
                // If hdbuserstore is not used to retrieve the connnection information, the format would be
                // "Server=;UID=User1;PWD=Password1;encrypt=true;sslValidateCertificate=false"
                using (var conn = new HanaConnection("key=User1UserKey;encrypt=true;sslValidateCertificate=false"))
                    var query = "SELECT TITLE, FIRSTNAME, NAME FROM HOTEL.CUSTOMER";
                    using (var cmd = new HanaCommand(query, conn))
                    using (var reader = cmd.ExecuteReader())
                        Console.WriteLine("Query result:");
                        // Print column names
                        var sbCol = new System.Text.StringBuilder();
                        for (var i = 0; i < reader.FieldCount; i++)
                        // Print rows
                        while (reader.Read())
                            var sbRow = new System.Text.StringBuilder();
                            for (var i = 0; i < reader.FieldCount; i++)
            catch (Exception ex)
                Console.WriteLine("Error - " + ex.Message);

    Save and close the Program.cs file after replacing the code.

    Note that the address, port, UID and PWD will be retrieved from the hdbuserstore.

    The above app makes use of some of the SAP HANA client .NET Core driver methods, such as HanaConnection. Connection details for this class can be found at Microsoft ADO.NET Connection Properties.

  5. Run the app:

    dotnet run

    Before running the program make sure to be in the directory where Program.cs is saved

    Result of running the app
Log on to answer question
Step 3: Debug the application
  1. If you have not already done so, download Visual Studio Code.

  2. If you have not already done so, in Visual Studio Code, choose File | Add Folder to Workspace, and then add the HANAClientsTutorial folder.

  3. Open the file Program.cs.

    C# Extension

    Visual Studio Code will recognize the cs file extension and will suggest installing the C# for Visual Studio Code extension. Click Install.

  4. Place a breakpoint.
    Place Breakpoint

  5. Select Run | Start Debugging.

  6. Choose the .NET Core environment.
    Select the environment to run

    Notice that the debug view becomes active and that the RUN option is .NET Core Launch.

    Notice that the program stops running at the breakpoint that was set.

    Observe the variable values in the leftmost pane. Step through code.

    VS Code Debugging

    For further information on debugging .NET Core apps consult Tutorial: Debug a .NET Core console application using Visual Studio Code and Instructions for setting up the .NET Core debugger.

Congratulations! You have now created and debugged a .NET Core application that connects to and queries an SAP HANA database.

Which of the following statements are true?

Next Steps

Back to top