Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql connection-string for localhost server

I am newbie in this .NET and please don't mind in answering my simple question. I am trying to write a windows application, where in I am using a localhost SQLserver for database.

I need to know what is the exact connection string for my localhost, if my server name looks like as below:

Data Source=HARIHARAN-PC\SQLEXPRESS;Initial Catalog=master;Integrated Security=True

should i need to give this same as connection string, or is something wrong in this syntax.

whn i try to open my connection. I am seeing error in opening connection.

How the format of connection string should be? any one please guide me.

I tried like this :

 private void button1_Click(object sender, EventArgs e)     {         string str = "Data Source=HARIHARAN-PC\SQLEXPRESS;Initial Catalog=master;Integrated Security=True" ;          SqlConnection con = new SqlConnection(str);         SqlCommand cmd = new SqlCommand();         SqlDataReader r;          cmd.CommandText = "SELECT * from Table1";         cmd.CommandType = CommandType.Text;         cmd.Connection = con;          con.Open();          r = cmd.ExecuteReader();          con.Close();       } 

This code errors out at con.Open();

like image 459
GB Hariharan Avatar asked Dec 27 '13 06:12

GB Hariharan


People also ask

What is the connection string for local SQL Server?

Server=myServerName,myPortNumber;Database=myDataBase;User Id=myUsername;Password=myPassword; The default SQL Server port is 1433 and there is no need to specify that in the connection string.


2 Answers

Using the default instance (i.e., MSSQLSERVER, use the DOT (.))

<add name="CONNECTION_STRING_NAME" connectionString="Data Source=.;Initial Catalog=DATABASE_NAME;Integrated Security=True;" /> 
like image 90
Robert Green MBA Avatar answered Sep 23 '22 07:09

Robert Green MBA


Choose a database name in Initial Catalog

Data Source=HARIHARAN-PC\SQLEXPRESS;Initial Catalog=your database name;Integrated Security=True" ; 

see more

like image 27
Nagaraj S Avatar answered Sep 21 '22 07:09

Nagaraj S