2016-03-06 158 views
0

首先,请原谅我,但是当谈到ASP.NET时我总是新手,因为我现在正在尝试学习它的一些内容。ASP.NET MVC无法从本地SQL Server Express数据库中检索数据

我正在关注ASP.NET MVC4的在线教程之一,但我正在使用Visual Studio 2015社区(MVC5)和SQL Server 2014 Express。

目标是使用Windows身份验证从本地SQL Server Express数据库检索数据。在这种情况下,雇员的身份,姓名,城市和性别。这时我收到以下错误:

An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: The underlying provider failed on Open.

数据库看起来是这样的 - ID作为int,其余为varchar(50)

enter image description here

Employee.cs位于型号:

[Table("tblEmployee")] 
public class Employee 
{ 
     public int EmployeeId { get; set; } 
     public string Name { get; set; } 
     public string Gender { get; set; } 
     public string City { get; set; } 
} 

EmployeeContext.cs

public class EmployeeContext : DbContext 
{ 
    public DbSet<Employee> Employees { get; set; } 
} 

EmployeeController.cs

public class EmployeeController : Controller 
{ 
     // GET: Employee 
     public ActionResult Details(int id) 
     { 
      EmployeeContext employeeContext = new EmployeeContext(); 
      Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id); 
      return View(employee); 
     } 
} 

视图 - Details.cshtml

@model MVCDemo.Models.Employee 

@{ 
    ViewBag.Title = "Employee Details"; 
} 

<h2>Employee Details</h2> 
<table style="font-family:Arial"> 
    <tr> 
     <td> 
      <b>Employee ID:</b> 
     </td> 
     <td> 
      @Model.EmployeeId 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <b>Name:</b> 
     </td> 
     <td> 
      @Model.Name 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <b>Gender:</b> 
     </td> 
     <td> 
      @Model.Gender 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <b>City:</b> 
     </td> 
     <td> 
      @Model.City 
     </td> 
    </tr> 
</table> 

最后,来自web.config在根连接字符串(I制成肯定)目录:

<add name="EmployeeContext" 
    connectionString="Data Source=DESKTOP-6RQ94N9\MAIN;Initial Catalog=Sample;Integrated Security=SSPI" 
    providerName="System.Data.SqlClient" /> 

默认connectionString="Data Source=.\SQLEXPRESS;根本没有工作。 无论如何,服务器资源管理器显示与数据库的成功数据连接,我可以浏览Visual Studio中的表及其列。

当我启动该项目,并尝试例如:

http://localhost/MVCDemo/Employee/Details/1 

该网站只是不断加载了一段时间,然后在上面提到的例外发生在

Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id); 

当我调试应用程序, employeeContext.Employees.Local.Count显示0,所以我猜没有数据正在检索。

非常感谢您的帮助!

回答

0

那么,在这种情况下,我已经通过创建具有SELECT权限的用户并使用登录凭证更新了我的连接字符串,从而实现了一种解决方法。

之后,我已经成功从数据库中检索到我需要的信息。感谢大家的帮助!

0

那么,如果使用该选项,请参阅How to: Access SQL Server Using Windows Integrated Security以获取详细信息。

因为您将Visual Studio作为“您”运行(您的 Windows用户帐户),所以您能够在Visual Studio(或SSMS)中以“你”的身份连接到SQL。您的Asp.Net站点不是

您可以使用SQL身份验证(改为),以便您的应用程序(ASP。Net)和数据存储(SQL)用户是分开的,因此可以单独管理,而不必处理(创建)实际的Windows用户。如果/当您进入生产/托管时(例如,Web服务器和SQL Server分开,没有Windows“域”等),这将更加明显。

至于连接问题看How to Troubleshoot Connecting to the SQL Server Database Engine

H个。

0

是否确定employeeContext.Employees.Local.Count甚至试图调用数据库?您需要检查SQL Server Express实例的名称。在开始菜单中的所有程序中使用Microsoft SQL Server (your version)转到您的文件夹,然后单击Sql Server Configuration Manager。在SQL Server Services选项卡中,您将看到SQL Server (NAME),如果在安装过程中没有更改名称,通常会显示SQL Server (SQLEXPRESS)
enter image description here 此外,如果您能够使用SQLEXPRESS名称从Visual Studio访问SQL Server,则可以从本地运行的ASP.NET网站轻松访问它。我的一个项目有以下连接字符串:

<add name="DatabaseContext" connectionString="Data Source=DESKTOP-111111N\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient" /> 

并请尽量让你的数据库是这样一个电话:employeeContext.Employees.Count()
我希望能帮到你!

UPDATE

也请检查什么的连接字符串在数据库中,Context类构造函数中设置为我的项目是base("DatabaseContext")

public class DatabaseContext : DbContext 
{ 
    public DatabaseContext() 
     : base("DatabaseContext") 
    { 
    } 

} 


并为您的项目将是base("EmployeeContext")

+0

检查它在Sql Server配置管理器中,我确实已将服务器名称更改为MAIN。此时,连接字符串如下所示:''但仍然没有结果。 – Dandry

+0

您使用什么名称从Visual Studio Server Explorer访问它? – Alex

+0

我正在使用./MAIN服务器,然后从下拉列表中选择我的'Sample'数据库。一切正常。在VS中,我可以轻松浏览表格数据。我也用'base(“EmployeeContext”)创建了这个空构造函数。没有变化。 – Dandry

相关问题