Subby
Subby

Reputation: 5480

Understanding MVC & SQL Server

I am following some tutorials and I'm completely lost on how MVC/SQL Server connect.

I am going through a tutorial and the person mentions that the following code automatically creates a database for me:

<connectionStrings>
   <add name="LibraryContext" 
        connectionString="Data Source = |DataDirectory|Library.sdf" providerName="System.Data.SqlServerCe.4.0"/>

Can you please tell me what the above code does in a simple and detailed way. What |DataDirectory|Library.sdf does and what SQL Server CE..etc...all of it.. PLEASE & THANK YOU!

Upvotes: 0

Views: 261

Answers (2)

marc_s
marc_s

Reputation: 754230

  • SQL Server CE is the Compact Edition of SQL Server - basically your whole database in a single file (the .sdf file) and the code to access it in a set of DLLs - no server installation needed.

    It works fine for most cases, but has limitations (no stored procedures etc.) - ErikEJ has a great comparison page comparing SQL Server CE to Express

  • the |DataDirectory| part of your connection string refers to the App_Data directory that's been created in your web site

    So in the end, the database connection goes to a file in

    (your web site root)\App_Data\Library.sdf
    

Upvotes: 4

Spikeh
Spikeh

Reputation: 3695

MVC is effectively a tiered presentation layer - it can act alone, or with any back end database.

The "code" you have posted above is not code - it's configuration information that's read from a configuration file (in your case, web.config). Configuration files are used to store and easily change settings in your applications.

A connection string is literally just a string that contains database credentials. The database would need to already exist somewhere (unless you decide to implement a code-first ORM like Entity Framework code-first). In your example, you're trying to connect to an existing SQL Server CE (Compact Edition) file. You would need to create a SQL Server CE file in your App_Data directory. I'm afraid I only use the full "server" version of SQL Server, so I can't help you there, unless I look it up!

Once your database exists, you can connect to it in a lot of different ways. My current preference is Entity Framework, though historically it's been via ADO.Net. I would Google "Hello World SQL Server", which should help you find a decent tutorial on where to start.

Upvotes: 2

Related Questions