Arianule
Arianule

Reputation: 9043

adding an exsiting Microsoft sql database to an asp.net project

I have been doing C# asp.net exercises for a while and ussually I would add an Access Database to the application Data folder of my project and then with my OleDbConnection string connect to the database and interact with the tables.

I however want to now add an existing Microsoft sql 2008 express edition database to my asp.net project(visual studio 2012) but am struggling to do this as it is not a case of simply adding the databse to the application data folder and making a connection using the connection string necessary.

What would the walkthrough or step by step procedure be for doing this?

Upvotes: 0

Views: 5375

Answers (2)

Kuzgun
Kuzgun

Reputation: 4737

You don't have to work with files if you have an existing MSSQL database. Simply add your connection string and you are ready to use. You have 2 options to include connection string, you can include it in web.config or you can include it to your cs file directly.

If you choose to include it to your cs file you should use SqlConnection class. In web.config, it should look something like this:

<connectionStrings>
<add name="constr" connectionString="Data Source=yourservername;Initial Catalog=yourdatabasename;User ID=youruserid;Password=yourpass" providerName="System.Data.SqlClient"/>
</connectionStrings>

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

What would the walkthrough or step by step procedure be for doing this?

  1. Install SQL Server (Full Edition) if not already installed (could be installed on the same machine as the web application or on a separate machine).
  2. Create a database inside this SQL Server.
  3. Create the tables inside the newly created database.
  4. Specify the connection string to this SQL server.

You could of course use an embedded database. For example VS 2012 comes with a LocalDB which stores the files inside the App_Data folder. When you create a new ASP.NET MVC 4 application using the internet template it will set everything up for you. But that's not a production ready database. Basically it sets a connection string pointing to the App_Data folder:

<connectionStrings>
  <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20130107093649;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication1-20130107093649.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>

and setting entity framework to use this provider:

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>

So when you are ready to deploy your application to production and have a running SQL Server instance all you need to do is change the connection string to point to this SQL instance.

Here's a nice article on MSDN with various connection strings that you could use based on the target database.

Upvotes: 2

Related Questions