0

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!

2 Answers 2

4
  • 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
    
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

Can you please show me how I can connect to a proper SQL SERVER 2008 R2. I have made a simple database in there using SQL servers IDE. I have an mvc application and I'm just thinking how to link them together. What's the best way? I keep hearing about linq/entity framework etc.
There are a lot of ways to do this. Even though EF is quicker and easier, I would recommend learning some ADO.Net so you can hopefully appreciate where EF comes from. There are a lot of examples on here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.