You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MVC5 Web应用连接SQL Server的ConnectionString属性及配置问题

Hey there! Let's tackle your MVC5 database connection issue first, then break down every part of that connection string so you know exactly what each piece does.

First, Fix the "Data Source Not Found" Error

Your error is almost certainly due to an outdated LocalDB instance name. (LocalDB)\v11.0 is the instance name for SQL Server 2012's LocalDB. If you've installed a newer version of SQL Server (2016 or later), the default auto-updating LocalDB instance is (LocalDB)\MSSQLLocalDB. Swap that into your connection string first—this should resolve the "data source not found" problem right away.

If you're unsure which instance you have, open Command Prompt and run sqllocaldb info to see all installed LocalDB instances, then use the active one.


Let's walk through every part of your <connectionStrings> configuration, from the parent node down to each individual parameter.

1. The <connectionStrings> Node

This is a dedicated section in Web.config for storing all your database connection configurations. Each <add> tag inside it represents a single database connection setup.

2. The <add> Tag's Core Attributes

  • name: This is the unique identifier for your connection string. Your MVC DbContext class (like MovieDBContext) will use this name to locate the correct connection settings. Double-check that the name in your code matches this exactly—typos here are a common gotcha.
  • providerName: Specifies the .NET database driver to use. System.Data.SqlClient is the official driver for SQL Server, so this stays the same unless you're working with a different database (like MySQL or PostgreSQL).

3. The connectionString Parameter Details

Your connection string is split into semicolon-separated key-value pairs. Here's what each one means:

Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\Movies.mdf; Integrated Security=True
  • Data Source: Defines the database server or instance to connect to. For LocalDB (SQL Server's lightweight dev-focused version), this is the instance name. For full SQL Server, this would be something like localhost, .\SQLEXPRESS, or a remote server's IP/hostname.
  • AttachDbFilename: Points to the physical SQL Server database file (.mdf) you want to attach. The |DataDirectory| placeholder automatically maps to your ASP.NET project's App_Data folder—this is the standard location for storing data files in web apps. So |DataDirectory|\Movies.mdf translates to App_Data\Movies.mdf.
  • Integrated Security=True: Enables Windows Integrated Authentication, meaning your app uses the current Windows user's credentials to connect to the database. No username/password is needed here. If you set this to False, you'd need to add User ID and Password parameters to specify a SQL Server account.

Quick Extra Tip

Make sure your project has an App_Data folder—if it doesn't, right-click your project in Visual Studio → Add → New Folder, name it App_Data, and either place your Movies.mdf file there or let Entity Framework auto-generate it when you run the app.


内容的提问来源于stack exchange,提问作者Hojat Moshkabadian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:33:49