14

Suppose I have created a SQL Server database called Database1.mdf in the App_Data folder in Visual Studio with a table called Names.

How could I establish a connection to read the table values using C#?

So far I've tried something like this:

SqlConnection conn = new SqlConnection("Server=localhost;Database=Database1;");

conn.Open();

// create a SqlCommand object for this connection
SqlCommand command = conn.CreateCommand();
command.CommandText = "Select * from Names";

But I get an error:

database not found/error connecting to database

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Tony
  • 1,739
  • 9
  • 26
  • 46

7 Answers7

15

In Data Source (on the left of Visual Studio) right click on the database, then Configure Data Source With Wizard. A new window will appear, expand the Connection string, you can find the connection string in there

Zoltan Toth
  • 46,038
  • 11
  • 115
  • 133
  • 3
    To display the data source window: On the menu bar, choose View, Other Windows, Data Sources (or choose the Shift+Alt+D keys). – ECH Apr 27 '15 at 13:07
8

If you use SQL authentication, use this:

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "User Id=UserName;" + 
     "Password=Secret;" + 
     "AttachDbFilename=|DataDirectory|Database1.mdf;"
conn.Open();

If you use Windows authentication, use this:

using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "Integrated Security=true;" + 
     "AttachDbFilename=|DataDirectory|Database1.mdf;"
conn.Open();
John Washam
  • 4,014
  • 4
  • 30
  • 43
Hassan Boutougha
  • 3,883
  • 1
  • 15
  • 16
  • If you are not using named instance, then simply use dot like this `"Data Source=."` – Arulraj Jun 06 '18 at 05:35
  • I used this for SQLEXPRESS `connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"` from MSDN here https://msdn.microsoft.com/en-us/library/jj653752%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396#sse – Hrvoje T Aug 04 '18 at 20:23
5

If you're using SQL Server express, change

SqlConnection conn = new SqlConnection("Server=localhost;" 
       + "Database=Database1;");

to

SqlConnection conn = new SqlConnection("Server=localhost\SQLExpress;" 
       + "Database=Database1;");

That, and hundreds more connection strings can be found at http://www.connectionstrings.com/

David
  • 70,778
  • 16
  • 128
  • 169
4
SqlConnection c = new SqlConnection(@"Data Source=localhost; 
                           Initial Catalog=Northwind; Integrated Security=True");
ablaze
  • 684
  • 7
  • 26
3

You try with this string connection

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|Database1.mdf;Database=dbname; Trusted_Connection=Yes;
Aghilas Yakoub
  • 27,871
  • 5
  • 44
  • 49
2

I like to use the handy process outlined here to build connection strings using a .udl file. This allows you to test them from within the udl file to ensure that you can connect before you run any code.

Hope that helps.

czuroski
  • 4,186
  • 9
  • 48
  • 85
1

Visual Studio 2019 (and probably a few previous versions).

  • View -> SQL Server Object Explorer
  • Top of the tree is 'SQL Server'
  • Under 'SQL Server', are couple of '(localdb)....'
  • Expand the (localdb)... -> Databases until you find your db.
  • Database Name (eg. Database1) -> Right-click -> Properties, and scroll the many properties (eg. "ANSI NULL Default"). Find the "Connection string" property, copy the value into your code, and you're running.
Stephen Hosking
  • 1,496
  • 16
  • 33