Skip to Content
0

HanaConnection

Jun 02, 2017 at 02:50 PM

95

avatar image

hello

i'm trying to connect directly to Hana database in Visual Studio C#

in example is below i did this without database name

HanaConnection conn = new HanaConnection("UserID=Admin;Password=Money123;Server=HumanResources:30015" ); 

when i tried to set database name for example

HanaConnection conn = new HanaConnection("UserID=Admin;Password=Money123;Server=HumanResources:30015; Database=SBOTEST");

I have the message : database SBOTEST does not exist

the only one way is to set in querry

HanaCommand cmd = new HanaCommand("SELECT \"CardCode\" FROM SBODEMOPL.OCRD", conn);

it works

but i would like to write without set SBOTEST.OCRD

does anyone knows how to do this ?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Bartosz Jarkowski Jun 02, 2017 at 03:07 PM
1

Hi,

I think it should work if you add parameter CS=<schema name> to you connection string.

Pozdrawiam

Bartosz

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thanks a lot Bartosz :) ->>> Dzięki Bartek, nie mogłem tego znaleźć nigdzie :)

It works. !

1
Keven Lachance Jun 06, 2017 at 08:27 PM
1

And you might want to use the HanaConnectionStringBuilder to build the connection string. similar class exists for odbc and mssql. they simplify things for common property (server, username) and also automatically escape special characters correctly. additionnally I have added code to set the application name, which can help you when you check session/connection statisitics in hana studio.

			//Using the SQL Connection string builder protect agains user injection to the connection string. For example, it will add " around a field when using special characters
			//Like ; which is protected as used to separate properties.
			//Additionnally on the hana database, we prevent a malicious user to use sql injection by inserting control caracters; in the username, or database
			//and then use the InitString property to execute untrusted sql command. 
			HanaConnectionStringBuilder connString = new HanaConnectionStringBuilder();
			connString.Server = this.Server;
			connString.UserName = this.Username;
			connString.Password = this.Password;
			if (!String.IsNullOrEmpty(this.Database))
			{
				connString.CurrentSchema = this.Database;
			}


			if (!String.IsNullOrEmpty(this.AppName))
			{
				//InitString property of the ConnectionString Works on 85.3
				connString.InitString = string.Concat("set 'APPLICATION' = '", this.AppName.Replace("'", "''"), "'");
			}

                        connection = new HanaConnection(connString.ConnectionString);


			connection.Open();



Share
10 |10000 characters needed characters left characters exceeded