You are on page 1of 18

12/12/2018 Bridging the gap between .

NET and HANA | SAP Blogs

Products
Products Industries
Industries Support
Support Training
Training Community
Community Developer
Developer Partner
Partner

About
About

 
Ask a Question Write a Blog Post Login

Bridging the gap between .NET and HANA


October 16, 2014 | 7,620 Views |

Former Member

SAP HANA

integration option for Microsoft Office SharePoint software | Business Intelligence | Database | hana | integration option for
microsoft sharepoint software | net | sap

share share tweet share like


2 Follow

With the advent of HANA SPS 08 circa Q2 2014, SAP finally released the
HANA data provider for Microsoft ADO.NET. Admittedly, this is not trousers
stirring news this but I am pleased to know that there is now an effective
way to allow the .NET framework to efficiently communicate with SAP
HANA. In the past, you could still do that but it was through the use of the
ODBC driver – there is nothing wrong with it if all you wanted to do was to

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 1/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

connect to SAP HANA and perform some simple SQL statements but we all
know SAP HANA if more than just an in memory database.

This article will to tease out some of the key concepts and features
introduced by SPS 08 on the client interfaces for ADO.NET. Data access
and manipulation will be the theme here today using C# and SAP’s new
.NET API – Sap.Data.Hana. If you are wondering what ADO.NET is, it is
Microsoft data access technology for the .NET framework using
programming languages such as C# or VB.NET.

I beg the question that you ask why is it important for SAP to release this
new API when ODBC is sufficient? Given that the standard ODBC
connection to SAP HANA is still a valid method to bridge the connection to
the database or any database for that matter, we need to understand the
core use of an ODBC driver itself. Microsoft’s primary goal when they
released the initial ODBC driver was to provide a vendor neutral way of
accessing stored data in any
machine. As you might have already guessed, the whole host of ODBC
driver that Microsoft currently supports spans vast and wide from databases
such as Oracle, Informix, Teradata and all the way to SAP HANA, just to
name a few. The result of building an ODBC driver to be as generic as
possible can come at a cost. Cost in terms of lost of performance,
unleveraged hidden features, missing core functionalities and best practices
surrounding a given database.

Although there are no official information from SAP detailing why a


developer should use the new API, one can only infer that SAP has put in a
lot of hard work to ensure that your .NET code communicates efficiently with
their HANA database. Take for example if your team were to deliver a .NET
solution specifically on Microsoft SQL Server, they will certainly use the
System.Data.SQLClient.SQLConnection object, on Oracle they would most
likely end up using the ODP.NET driver and with SAP HANA, it would be
this new API. Although the .NET framework is able to connect to SAP
HANA through the ODBC driver, SAP reckons that your application will take
full advantage of all the underlying advances build specially for the .NET
framework to play nicely with SAP HANA. There will be instances when you
find yourself with no vendor delivered driver and in this case the ODBC
driver is your only option to exchange information with the database on the
.NET framework e.g. when developing application on Hadoop Hive.

Getting Started

To start using the new ADO.NET API delivered in SPS 08, you will need to
have the latest SAP HANA Client Developer Edition version 1.00.80

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 2/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

installed on your machine or alternatively check the default installation path


under C:\Program Files\sap\hdbclient\ado.net. If you have that installed, you
should see two folders call v3.5 and v4.5 and clicking through v4.5, you
should have the Sap.Data.Hana.v4.5.dll.

View, Insert, Update and Delete

Building an enterprise .NET application on SAP HANA will undoubtedly


require you to perform one of these four actions and in this section of the
article, the aim is to demonstrate the basic database call to interact and
manipulate the data that resides in SAP HANA.

Under the .NET solution explorer, expend the Reference dropdown and
ensure that Sap.Data.Hana.v4.5 has been included to your .NET solution
and if not you can add a new reference under the Extensions path. Once
you have that, standard C #codes ensue for namespace: using
Sap.Data.Hana;

Establishing the database connection

The bare minimum parameter required to establish a connection is to have


the connection string include the server address, user name and password.
Other parameters such as connection lifetime, database, pooling, etc. are
optional but worthwhile exploring if you want to maintain a healthy level of
database activity.

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 3/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

Selecting records

Once a database connection has been established it is a straight forward


approach to acquire the data that you require from any table or schema. In
this example, I have issued a simple SELECT statement with the intention
of publishing the data to a .NET DataGridViewer. The class demonstrated
here is the HanaDataReader class use to return a read only result set and
output into a Windows form.

//Establish an active connection to the SAP HANA database

HanaConnection conn = new


HanaConnection(“Server=your.hana.server;UserID=username;Password=pa
ssword”);

//Open the database connection

conn.Open();

String strSQL = “select * from customers order by ID desc”;

//Execute the SQL statement

HanaCommand cmd = new HanaCommand(strSQL, conn);

//Read and store the result set

HanaDataReader reader = cmd.ExecuteReader();

//Bind the result set to a DataGridViewer/

dgViewTweet.DataSource = reader;

//Close the reader connection

reader.Close();

//Close the database connection

conn.Close();

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 4/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

Updating new records

Inserting records into the database is as easy as querying for it but not
without additional effort to format and prepare the data. The example
provided here stores the information from 5 text fields using standard SQL
inserts statements executed by C#.

Upon loading the application, it connects to SAP HANA, loads 150 rows of
customer records from the database and the intention here is to create a
new piece of information and write it back to the table.

When the Save New button is clicked, the .NET framework runs through
this piece of simple code to perform two sets of activities. Firstly, it will
perform a SQL insert statement to store all the values captured from the
https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 5/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

text box by running the ExecuteNonQuery() method and secondly it reread


the information by running the ExecuteReader() method as this will
repopulate the DataGridViewer with the new record.

The only information that the ExecuteNonQuery() method returns is the


number of rows affected by the specific action and this method is useful for
running SQL statements such as INSERT, UPDATE or DELETE.

HanaConnection conn = new


HanaConnection(“Server=your.hana.server;UserID=username;Password=pa
ssword”);

string sCust, sLifespend, sNewspend, sIncome, sLoyalty;

conn.Open();

HanaCommand insertCmd = new HanaCommand(“INSERT INTO


Customers(CUSTOMER, LIFESPEND, NEWSPEND, INCOME, LOYALTY) “ +
“VALUES(?, ?, ?, ?, ? )”, conn);

sCust = txtCustomer.Text;

sLifespend = txtLifespend.Text;

sNewspend = txtNewspend.Text;

sIncome = txtIncome.Text;

sLoyalty = txtLoyalty.Text;

HanaParameter parm = new HanaParameter();

//Customer

parm = new HanaParameter();

parm.HanaDbType = HanaDbType.NVarChar;

insertCmd.Parameters.Add(parm);

//Life Spend

parm = new HanaParameter();

parm.HanaDbType = HanaDbType.Double;

insertCmd.Parameters.Add(parm);

//New Spend

parm = new HanaParameter();

parm.HanaDbType = HanaDbType.Double;

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 6/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

insertCmd.Parameters.Add(parm);

//Income

parm = new HanaParameter();

parm.HanaDbType = HanaDbType.Double;

insertCmd.Parameters.Add(parm);

//Loyalty

parm = new HanaParameter();

parm.HanaDbType = HanaDbType.Double;

insertCmd.Parameters.Add(parm);

//Customers

insertCmd.Parameters[1].Value = sCust;

//Life Spend

insertCmd.Parameters[2].Value = sLifespend;

//New Spend

insertCmd.Parameters[3].Value = sNewspend;

//Income

insertCmd.Parameters[4].Value = sIncome;

//Loyalty

insertCmd.Parameters[5].Value = sLoyalty;

//Execute the insert statement

insertCmd.ExecuteNonQuery();

//Requery for the latest record from SAP HANA

String strSQL = “select * from customers order by ID desc”;

//Execute the SQL statement

HanaCommand cmd = new HanaCommand(strSQL, conn);

//Read and store the result set

HanaDataReader reader = cmd.ExecuteReader();

//Bind the result set to a DataGridViewer/

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 7/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

dgViewTweet.DataSource = reader;

//Close the reader connection

reader.Close();

//Close the database connection

conn.Close();

Using Stored Procedure

My guess is Stored Procedure has always been off limits for SAP
applications that sits on the NetWeaver stack because of the vast database
vendors out there and the effort to cater for the different method of creating
and calling a procedure can add up to an unnecessary amount of overhead.
What SAP has given us in equivalent is the lovely function module builder
that we have all known to work with and love throughout the years. Like any
high performing, respectable RDBMS out there in the market, SAP HANA
has included the Stored Procedure function and using it with .NET is just as
simple as the
rest.

In this next example, the goal here is to change the record for Customer ID 150.
The customer name Samuel Johnson has been misspelled and it needs to be
corrected. Upon clicking on the Update Changes bu on, the .NET framework
https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 8/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

will call a Stored Procedure in SAP HANA, passing in the Customer ID and
update the record using a standard SQL update command that is embedded
within a Stored Procedure. A Stored Procedure can contain logic as simple as
accep ng fields and processing it to as complex as performing specific
valida on and returning an error if the condi ons are not met.

CREATE PROCEDURE prcUpdateCustomers(IN ID INT, IN CUSTOMER


NVARCHAR(60)) AS

BEGIN

UPDATE customers SET customer = :CUSTOMERWHERE ID = :ID;

END;

The change to execute Stored Procedure is to set the Hana command type
to StoredProcedure and passing in the actual name of the Stored
Procedure.

HanaCommand cmd = new HanaCommand(“”, conn);

//Pass in the Stored Procedure Name

cmd.CommandText = “prcUpdateCustomers”;

cmd.CommandType = CommandType.StoredProcedure;

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 9/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

//Read the changed column data

string col0 = dgViewTweet[0,


dgViewTweet.CurrentCell.RowIndex].Value.ToString();

string col1 = dgViewTweet[1,


dgViewTweet.CurrentCell.RowIndex].Value.ToString();

string col2 = dgViewTweet[2,


dgViewTweet.CurrentCell.RowIndex].Value.ToString();

string col3 = dgViewTweet[3,


dgViewTweet.CurrentCell.RowIndex].Value.ToString();

//Establish an active connection to the SAP HANA database

HanaConnection conn = new


HanaConnection(“Server=your.hana.server;UserID=username;Password=pa
ssword”);

//Open the database connection

conn.Open();

//Create an instance of a HanaCommand

HanaCommand cmd = new HanaCommand(“”, conn);

//Pass in the Stored Procedure Name

cmd.CommandText = “schia_pal.prcUpdateCustomers”;

cmd.CommandType = CommandType.StoredProcedure;

//Prepare input parameters

HanaParameter param = new HanaParameter();

param = cmd.CreateParameter();

param.HanaDbType = HanaDbType.Integer;

param.Direction = ParameterDirection.Input;

param.Value = col0;

cmd.Parameters.Add(param);

param = new HanaParameter();

param = cmd.CreateParameter();

param.HanaDbType = HanaDbType.NVarChar;

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 10/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

param.Direction = ParameterDirection.Input;

param.Value = col1;

cmd.Parameters.Add(param);

//Execute he update statement

cmd.ExecuteNonQuery();

cmd.Dispose();

conn.Close();

In brief and not trying to include needless details on the vast .NET sample
codes which SAP has documented for the wider community, this article is
just an example of some of the basic database communication that you can
work with using Sap.Data.Hana. With the new SPS 08 client interface,
customers who are heavily reliant on the .NET framework to address
business problems can now have full SAP support when working with SAP
HANA and the ability to fully utilise all features within the product.

Alert Moderator

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 11/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

12 Comments
You must be Logged on to comment or reply to a post.

Former Member

March 3, 2015 at 9:51 am

Hello Simon,

A quick question, is it possible to call HANA Stored Procedure which accepts table
input parameter from .NET via the ADO.Net connector?. Looked at the examples but
all of them are related to a calling with single input values rather than table input
values.

My use case is that I want to send a bunch of records(ID, user, dates, etc) to a stored
procedure which insert or update these records in a DB table. It does some
validations before the insert/update operations all that logic is in the stored
procedure.

Any inputs here is greatly appreciated.

Best Regards,

Kiran

Lars Breddemann

May 12, 2015 at 7:56 am

Table typed input parameters are not supported for client calls. This
applies to all clients (JDBC, ODBC, ADO.net… ).

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 12/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

If you need to transfer a whole table content you need to look into other
options like filling temporary tables.

– Lars

Former Member

May 14, 2015 at 11:30 am

Hello Lars,

Thanks for your inputs about usage of table-type


parameters.

I am new to HANA.

I am migrating Stored Procedures from SQL to HANA.

I have successfully connected HANA database in .Net


environment and I’m able to extract records from a Stored
Procedure using HanaConnection class in sap.data.hana
dll.

But there are some SPs for which I need to supply a table-
type output parameter and there are some SPs where I
need to supply a table-type input parameter.

For the input parameter as a table-type, I need to send


bulk of records from .Net environment onto Hana
database. In SQL, that was possible using User-defined
Table types and specifying the respective input parameter
in the SP as that table-type.

But, how can I do it in HANA? You mentioned about filling


temporary tables, but how to fill them using data that I need
to send from .Net?

Secondly, for the SP where I have specified a table-type


output parameter, I defined the parameter values in my
.Net end as:-

param = new HanaParameter();

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 13/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

param = cmd.CreateParameter();

param.Direction = ParameterDirection.Output;

param.HanaDbType =
HanaDbType.TableType;

param.Value = “?”;

along with other input parameters which are Integer values.


When I ran the code, the control gave error on the
statement “param HanaDbType =
HanaDbType.TableType;”, stating that “The feature is not
supported”.

So, I’m not able to specify table-type as an input as well as


an output variable.

Any help regarding this is greatly appreciated. Thanks!

Kind Regards

Vijayesh

Lars Breddemann

May 17, 2015 at 10:07 am

Hey Vijayesh,

in order to use the table-typed output you


need to fetch the corresponding result set(s).

As mentioned before SAP HANA currently


doesn’t support table typed parameters for
client-server communication.

– Lars

Mohan Chandra

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 14/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

September 10, 2018 at 7:57 am

Hi, Can I have Oledb connection string like I am using for SQL server

<add name=”ERPSourceStr”
connectionString=”Provider=SQLOLEDB;Data
Source=DataSource;Initial Catalog=DatabaseName;User
ID=UserName;Password=*******; Integrated Security=SSPI”/>

And To use Oracle I am using Oledb connection string

<add name=”ERPSourceStr”
connectionString=”Provider=OraOLEDB.Oracle;Data Source=
(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=
(PROTOCOL=TCP)(HOST=Host)(PORT=1521)))(CONNECT_DATA=
(SID=xe)(SERVER=DEDICATED)));User ID=User;Password=******;” />

Same I want connection string for SAP Hana Studio

please Can you provide?

Thanks in Advance

Former Member

November 30, 2015 at 4:31 pm

Hi,

i have install rel 97.0 and the binding with grid and dropdownlist don’t work, witth the
85.3 i havn’t problem.

help me!!!

Former Member

November 30, 2015 at 4:44 pm

Solved,

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 15/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

i use the adapter to fill datatable and i have bind the datatablel to dropdownlist

Former Member

October 10, 2016 at 1:59 pm

Hi,
I would like to make progrmming a small application on the motorola scanner , the
system is window CE6.0 and have to use vs2008 to develop, but the framework is
.net3.5 not .net4.5 in vs2008, my question is is it possible to connect to SAP hana
database using vs2008 with .net3.5, how to do with it? thank you very much!

Former Member

October 31, 2016 at 6:53 am

during debug the application there is an error “External component has thrown an
exception” on the row of HanaDataReader reader = cmd.ExecuteReader();, do you
know whats problem?

Ricardo Renteria

November 1, 2016 at 8:39 pm

Edison, in that OS do you have installed the ADO .NET API for
connection to HANA database as Simon mention in Getting Started
section? Do you have the reference to Sap.Data.Hana in your code?

Ricardo Renteria

November 1, 2016 at 9:16 pm

Hi Simon,

I see you use the connection string with the parameters needed to establish the
connection with the HANA DB server, but I don’t see the schema in which run the
https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 16/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

queries.

If I run a query only with those parameters I get the error “the table XXX does not
exist in the schema SYSTEM”; the same occurs if I try to add the “Current
schema=MYHANADATABASE” parameter to the connection string. Despite I set the
schema, the query looks like still address the SYSTEM schema.

How to solve this?

Mohan Chandra

September 10, 2018 at 9:48 am

Hi, Can I have Oledb connection string like I am using for SQL
server

<add name=”ERPSourceStr”
connectionString=”Provider=SQLOLEDB;Data
Source=DataSource;Initial Catalog=DatabaseName;User
ID=UserName;Password=*******; Integrated Security=SSPI”/>

And To use Oracle I am using Oledb connection string

<add name=”ERPSourceStr”
connectionString=”Provider=OraOLEDB.Oracle;Data Source=
(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=
(PROTOCOL=TCP)(HOST=Host)(PORT=1521)))
(CONNECT_DATA=(SID=xe)(SERVER=DEDICATED)));User
ID=User;Password=******;” />

Same I want connection string for SAP Hana Studio

please Can you provide?

Thanks in Advance

I am not able to install SAP hana studio also in my system

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 17/18
12/12/2018 Bridging the gap between .NET and HANA | SAP Blogs

Share & Follow

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Cookie Preferences

Sitemap Newsletter

https://blogs.sap.com/2014/10/16/bridging-the-gap-between-net-and-hana-using-c/ 18/18

You might also like