cancel
Showing results for 
Search instead for 
Did you mean: 

HanaConnection

0 Kudos

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 ?

Accepted Solutions (1)

Accepted Solutions (1)

BJarkowski
Active Contributor

Hi,

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

Pozdrawiam

Bartosz

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

It works. !

Answers (1)

Answers (1)

former_member184713
Participant

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();