Alex Rivera | Logout

Connecting to local SQL Server database using C#

Asked 2012-08-31T18:36:23.040
13

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

Edit
Report

1 Answer

4
SqlConnection c = new SqlConnection(@"Data Source=localhost; 
                           Initial Catalog=Northwind; Integrated Security=True");
answered 2013-01-11T18:18:25.250

Your Answer