71

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

svick
  • 236,525
  • 50
  • 385
  • 514
GB Hariharan
  • 927
  • 2
  • 7
  • 6
  • 1
    Which type of "authentication mode" you are using ?? Is it "Windows Authentication mode" or "SQL Server mode (Mixed mode)" ?? You should use **user id** and **password** in your connection string. – Krishnraj Rana Dec 27 '13 at 07:14
  • 1
    What kind of error are you seeing? – svick Dec 27 '13 at 11:26

13 Answers13

108

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;" />
Robert Green MBA
  • 1,834
  • 1
  • 22
  • 45
21

Choose a database name in Initial Catalog

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

see more

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
  • 2
    [this](http://stackoverflow.com/questions/5283727/what-is-the-sql-connection-string-i-need-to-use-to-access-localhost-sqlexpress-w) post worked for me to connect to **local** using `localhost\SQLEXPRESS` – Shaiju T Dec 06 '15 at 07:05
16

You can also use Dot(.) for local key i.e;

Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True

If you have the default server instance i.e. MSSQLSERVER, then just use dot for Data Source.

Data Source=.;Initial Catalog=master;Integrated Security=True
Aftab Ahmed
  • 665
  • 9
  • 17
9

Do You have Internal Connection or External Connection. If you did Internal Connection then try this:

"Data Source=.\SQLEXPRESS;AttachDbFilename="Your PAth .mdf";Integrated Security=True;User Instance=True";
Yick Leung
  • 1,048
  • 1
  • 9
  • 16
Akil M Belim
  • 115
  • 9
8

Try this connection string.

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

See this link for more details http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.110%29.aspx

Ajay
  • 6,418
  • 18
  • 79
  • 130
8
<add name="connstr" connectionString="Data Source=localhost;Initial Catalog=DBName;User Id=username;Password=password" providerName="System.Data.SqlClient"/>

The above also works. It ignores the username and password passed in in the connection string. I switched from an environment db to a local one, and it works fine even though my user in the connection string does not exist in this context.

Vishav Premlall
  • 456
  • 6
  • 22
5

In .Net configuration I would use something like:

"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=..."

This information is from https://www.connectionstrings.com/sql-server-2016/

Jari Turkia
  • 1,184
  • 1
  • 21
  • 37
  • Also see difference between SQL Express and SQL Server Developer Edition: https://stackoverflow.com/a/61659580/1548275 – Jari Turkia Nov 03 '22 at 15:50
4

use this connection string :

Server=HARIHARAN-PC\SQLEXPRESS;Intial Catalog=persons;Integrated Security=True;

rename person with your database name

Ram Singh
  • 6,664
  • 35
  • 100
  • 166
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
3
string str = @"Data Source=HARIHARAN-PC\SQLEXPRESS;Initial Catalog=master;Integrated Security=True" ;
Mahmoud
  • 39
  • 1
3
public string strConnectionstring = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\DataBaseName.mdf";
StealthRT
  • 10,108
  • 40
  • 183
  • 342
2
string str = "Data Source=HARIHARAN-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True" ;
1

When using SQL Express, you need to specify \SQLExpress instance in your connection string:

string str = "Data Source=HARIHARAN-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True" ;
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
0
Data Source=HARIHARAN-PC\SQLEXPRESS; Initial Catalog=Your_DataBase_name; Integrated Security=true/false; User ID=your_Username;Password=your_Password;

To know more about connection string Click here

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62