Suedish
Suedish

Reputation: 385

How to add SQL Server database to my VS11 Website?

I have a website project (C#/ASP.NET) opened in Visual Studio 11 (beta) which works with the built-in SQL Server Compact Edition. But, I would rather use my SQL Server 2012 which is installed on this machine, and I have my tables already created in it.

Question is, how do connect to it from VS11? Do I add it in the App_Data folder where I have the Compact database?

Right now I am opening my pre-made database with the

var db = Database.Open("StarterSite");

command.

Upvotes: 1

Views: 1178

Answers (2)

Todd
Todd

Reputation: 1

It will depend on the technique you wish to use to connect to the SQL server database. I prefer to set my connection string in the web.config and use that when connecting to my database. There are several options you might want to explore. Here is a good MSDN article explaining some techniques.

Connecting to Databases in ASP.NET

Upvotes: 0

marc_s
marc_s

Reputation: 754220

If you already have the database created on SQL Server 2012, then just use it!

No need to add it to your project (and most definitely don't copy it into App_Data!).

Just create a connection string in your web.config something like:

<connectionStrings>
   <add name="YourConnectionStringNameHere"
        connectionString="server=(local);database=YourDatabase;Integrated Security=SSPI;" />
</connectionStrings>

and then use that in your app using all the normal ADO.NET components like SqlConnection or SqlCommand:

string connectionString = WebConfigurationManager.ConnectionStrings["YourConnectionStringNameHere"].ConnectionString;

using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("your-sql-statement-here", conn))    
{
   // do work in your database here....
}

Or use something like Entity Framework to make your life even easier!

Upvotes: 1

Related Questions