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
  • 732,580
  • 175
  • 1,330
  • 1,459
Tony
  • 1,839
  • 10
  • 27
  • 48

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,981
  • 12
  • 120
  • 134
  • 3
    To display the data source window: On the menu bar, choose View, Other Windows, Data Sources (or choose the Shift+Alt+D keys). – Ed Harrod 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,073
  • 4
  • 32
  • 43
Hassan Boutougha
  • 3,871
  • 1
  • 17
  • 17
  • 1
    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
  • 72,686
  • 18
  • 132
  • 173
4
SqlConnection c = new SqlConnection(@"Data Source=localhost; 
                           Initial Catalog=Northwind; Integrated Security=True");
ablaze
  • 722
  • 7
  • 30
3

You try with this string connection

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|Database1.mdf;Database=dbname; Trusted_Connection=Yes;
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
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,316
  • 9
  • 50
  • 86
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,405
  • 16
  • 34