Connect Using the SAP HANA .NET Interface
- How to install the .NET SDK
- How to create and debug a .NET application that queries an SAP HANA database
Prerequisites
- You have completed the first 3 tutorials in this mission.
.NET is a free and open-source software framework for Microsoft Windows, Linux and macOS operating systems and is the successor to the .NET Framework. .NET was previously known as .NET Core.
- Step 1
The first step is to check if you have the .NET SDK installed and what version it is. Enter the following command:
ShellCopydotnet --versionIf the
dotnetcommand is not recognized, it means that the .NET SDK has not been installed. If the SDK is installed, the command returns the currently installed version, such as 8.0.203.If the .NET SDK is not installed, download it from Download .NET and run the installer on Microsoft Windows or Mac.

On Linux, follow the instructions for the appropriate Linux version such as Install the .NET SDK or the .NET Runtime on openSUSE.
In order for the shell to recognize that the .NET SDK is installed and for any
dotnetcommands in future steps to be recognized, a new shell window needs to be opened.For further details on supported versions, see SAP Note 3165810 - SAP HANA Client Supported Platforms.
- Step 2
-
Create a new console app with the below commands:
Shell (Microsoft Windows)Copycd %HOMEPATH%/HANAClientsTutorial dotnet new console -o dotNETOn Linux or Mac, you need to modify the
HDBDOTNETCOREvariable to point to the location of thelibadonetHDB.soorlibadonetHDB.dylibfile before creating a new console app.There are two ways to set an environment variable: using the
EXPORTcommand on a Shell window or in a user’s profile script. When an environment variable is modified from the Shell, however, its existence ends when the user’s session ends. This could become an issue when you want the variable to persist across multiple user sessions.Hence, we will set the
HDBDOTNETCOREenvironment variable via the user profile.Open an editor to edit the file
.bash_profileor.profile.Shell (Linux or Mac)Copypico ~/.bash_profileReplace
picowith your preferred text editor.Add the following line to it.
Shell (Linux or Mac)Copyexport HDBDOTNETCORE=/home/dan/sap/hdbclient/dotnetcoreRun the source command to immediately apply all the changes made to the
.bash_profilefile.Shell (Linux or Mac)Copysource ~/.bash_profileNow, you may run the following command to create the console app.
Shell (Linux or Mac)Copycd $HOME/HANAClientsTutorial dotnet new console -o dotNET -
Open the
dotNET.csprojfile:Shell (Microsoft Windows)Copycd dotNET notepad dotNET.csprojShell (Linux or Mac)Copycd dotNET pico dotNET.csprojAdd the following below the
PropertyGroupsection (within theProjectsection) to indicate where to load the SAP HANA Client .NET driver from. Modify theHintPathsection with the information about where the dll is located on your machine.The SAP HANA driver can be downloaded from SAP Development Tools for either Linux, Windows or macOS if required.
Shell (Microsoft Windows)Copy<ItemGroup> <Reference Include="Sap.Data.Hana.Core.v6.0"> <HintPath>C:\SAP\hdbclient\dotnetcore\v6.0\Sap.Data.Hana.Net.v6.0.dll</HintPath> </Reference> </ItemGroup>Shell (Linux or Mac)Copy<ItemGroup> <Reference Include="Sap.Data.Hana.Core.v6.0"> <HintPath>/home/dan/sap/hdbclient/dotnetcore/v6.0/Sap.Data.Hana.Net.v6.0.dll</HintPath> </Reference> </ItemGroup>
Once the
dotNet.csprojfile has been updated, save and close the file. -
Run the app to validate that SAP HANA driver can be loaded:
ShellCopydotnet runIf an error occurs, double check that the hintpath is correct.
-
Open an editor to edit the file
Program.cs.Shell (Windows)Copynotepad Program.csShell (Linux or Mac)Copypico Program.cs -
Replace all content of
Program.cswith the code below. Be sure to update values where necessary and save the file when finished.C#Copyusing System; using Sap.Data.Hana; namespace dotNETQuery { class Program { static void Main(string[] args) { try { using (var conn = new HanaConnection("Server=999deec0-ccb7-4a5e-b317-d419e19be648.hana.prod-us10.hanacloud.ondemand.com:443;UID=User1;PWD=Password1;encrypt=true;sslValidateCertificate=false")) // encrypt and sslValidateCertificate should be true for HANA Cloud connections // As of SAP HANA Client 2.6, connections on port 443 enable encryption by default // sslValidateCertificate should be set to false when connecting // to an SAP HANA, express edition instance that uses a self-signed certificate. { conn.Open(); Console.WriteLine("Connected"); var query = "SELECT TITLE, FIRSTNAME, NAME FROM HOTELS.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++) { sbCol.Append(reader.GetName(i).PadRight(20)); } Console.WriteLine(sbCol.ToString()); // Print rows while (reader.Read()) { var sbRow = new System.Text.StringBuilder(); for (var i = 0; i < reader.FieldCount; i++) { var result = reader[i].ToString(); if (result is not null) { sbRow.Append(result.PadRight(20)); } } Console.WriteLine(sbRow.ToString()); } conn.Close(); } } } catch (Exception ex) { Console.WriteLine("Error - " + ex.Message); Console.WriteLine(ex.ToString()); } } } }Save and close the
Program.csfile after replacing the code and updating the host and port.The above app makes use of some of the SAP HANA client .NET driver methods, such as HanaConnection. Connection details for this class can be found at Microsoft ADO.NET Connection Properties. Further .NET API details can be found in the .NET API browser.
-
Run the app:
ShellCopydotnet runBefore running the program make sure to be in the directory where Program.cs is saved

-
- Step 3
-
Open Visual Studio Code. If needed, download Visual Studio Code here.
-
If you have not already done so, choose File | Add Folder to Workspace, and then add the
HANAClientsTutorialfolder.
-
Open the file
Program.cs.
Visual Studio Code will recognize the
csfile extension and will suggest installing the C# for Visual Studio Code extension. Click Install. -
Place a breakpoint on the line sbRow.Append line. Select Run | Start Debugging | .NET Core. A configuration will be added. Choose Run | Start Debugging.
Notice that the debug view becomes active and that the RUN option is .NET Launch.
Notice that the program stops running at the breakpoint that was set.
Observe the variable values in the leftmost pane. Step through code.

For further information on debugging .NET apps consult Tutorial: Debug a .NET Core console application using Visual Studio Code and Instructions for setting up the .NET Core debugger.
-
- Step 4
Congratulations! You have now created and debugged a .NET application that connects to and queries an SAP HANA database.
Which of the following statements are true?