MVC 3 - Using SQL Server Express



我是一般使用c#和MVC的初学者,我一直在遵循MVC音乐商店教程,因为我的作业问题类似于教程(它是一个商店)。然而,我遇到了一个问题。我需要使用SQL Server Express的数据库,而不是SQL Server Compact.

我更改了连接字符串,当它编译时它不工作..

<add name="FashionStyle" connectionString="Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|FashionStyle.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

在我的StoreController:

public ActionResult Index()
{
    var types = storeDB.Types.ToList();
    return View(types);
}

视图:

<h3>Browse Type of Product</h3>
<p>
    Select from @Model.Count() type:</p>
<ul>
    @foreach (var type in Model)
    {
        <li>@Html.ActionLink(type.Name, "Browse", new { type = type.Name })</li>
    }
</ul>

同样,当我运行并导航到商店页面时,会显示"Browse Type of Product Select from 0 Type:"。我还使用了教程

中修改过的sampledata.cs

您的连接字符串错误。

AttachDbFilename=|DataDirectory|FashionStyle.mdf写成Initial Catalog=[DB-NAME],用数据库的名称代替[DB-NAME]

有关更多连接字符串参考,您可以查看此站点:http://www.connectionstrings.com/sql-server-2008

ConnectionString:

Server=.SQLExpress;AttachDbFilename=c:asdqwemydbfile.mdf;Database=dbname; Trusted_Connection=Yes; 

最新更新