C# ASP.NET MVC无法从本地SQL Server Express数据库检索数据
首先,请原谅,但我是一个完全的新手,当谈到ASP.NET,因为我正在努力学习它的一些时刻 我正在学习ASP.NET MVC4的一个在线教程,但我使用的是Visual Studio 2015社区(MVC5)和SQL Server 2014 Express 目标是使用Windows身份验证从本地SQL Server Express数据库检索数据。在本例中,员工必须填写其id、姓名、城市和性别。此时,我得到以下错误: EntityFramework.SqlServer.dll中发生类型为“System.Data.Entity.Core.EntityException”的异常,但未在用户代码中处理 其他信息:基础提供程序在打开时失败 数据库如下所示-ID为C# ASP.NET MVC无法从本地SQL Server Express数据库检索数据,c#,asp.net,sql-server,asp.net-mvc-5,C#,Asp.net,Sql Server,Asp.net Mvc 5,首先,请原谅,但我是一个完全的新手,当谈到ASP.NET,因为我正在努力学习它的一些时刻 我正在学习ASP.NET MVC4的一个在线教程,但我使用的是Visual Studio 2015社区(MVC5)和SQL Server 2014 Express 目标是使用Windows身份验证从本地SQL Server Express数据库检索数据。在本例中,员工必须填写其id、姓名、城市和性别。此时,我得到以下错误: EntityFramework.SqlServer.dll中发生类型为“System
int
,其余为varchar(50)
:
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; }
}
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);
}
}
@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>
视图-详细信息.cshtml:
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
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);
}
}
@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>
默认的connectionString=“Data Source=。\SQLEXPRESS;
根本不起作用。
无论如何,服务器资源管理器显示了到数据库的成功数据连接,我可以在VisualStudio中浏览表及其列
当我启动项目并尝试时,例如:
http://localhost/MVCDemo/Employee/Details/1
该站点只是保持加载一段时间,然后上述异常在
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
调试应用程序时,employeeContext.Employees.Local.Count
显示0,因此我猜没有检索到数据
非常感谢您的帮助!如果使用该选项,请查看详细信息
您可以在Visual Studio(或SSMS)中以“您”的身份连接到SQL,因为您以“您”(您的Windows用户帐户)的身份运行Visual Studio。您的Asp.Net站点不是
您可以使用SQL身份验证,这样您的应用程序(ASP.Net)和数据存储(SQL)用户是分开的,因此可以分开管理,而不必处理(创建)实际的Windows用户。如果/当您进入生产/托管(例如,web服务器和SQL服务器是分开的,没有Windows”域,这将更加明显"等)
有关连接问题,请参见
Hth.如果使用该选项,请参阅以了解详细信息
您可以在Visual Studio(或SSMS)中以“您”的身份连接到SQL,因为您以“您”(您的Windows用户帐户)的身份运行Visual Studio。您的Asp.Net站点不可用
您可以使用SQL身份验证,这样应用程序(ASP.Net)和数据存储(SQL)用户是分开的,因此可以单独管理,而不必处理(创建)实际的Windows用户。如果/当您进行生产/托管时(例如,web服务器和sql server是分开的,没有Windows“域”等),这一点会更加明显
有关连接问题,请参见
Hth.您确定
employeeContext.Employees.Local.Count
甚至尝试调用数据库吗?您需要的是检查SQL Server Express实例的名称。在“开始”菜单的“所有程序”中,使用Microsoft SQL Server(您的版本)转到文件夹,然后单击“SQL Server配置管理器”。在SQL Server服务
选项卡中,您将看到SQL Server(名称)
,如果您在安装过程中没有更改名称,通常会显示SQL Server(SQLEXPRESS)
。此外,如果可以使用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" />
对于您的项目,它将是
base(“EmployeeContext”)
您确定EmployeeContext.Employees.Local.Count
甚至尝试调用数据库吗?您需要的是检查SQL Server Express实例的名称。在“开始”菜单的“所有程序”中,使用Microsoft SQL Server(您的版本)转到文件夹,然后单击“SQL Server配置管理器”。在SQL Server服务
选项卡中,您将看到SQL Server(名称)
,如果您在安装过程中没有更改名称,通常会显示SQL Server(SQLEXPRESS)
。此外,如果可以使用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" />
对于您的项目,它将是
base(“EmployeeContext”)
好的,在这种情况下,我实际上已经通过创建一个具有SELECT权限的用户并使用登录凭据更新我的连接字符串来解决问题
之后,我成功地从数据库中检索到了所需的信息。谢谢大家的帮助 在这种情况下,我实际上已经通过创建一个具有SELECT权限的用户并使用登录凭据更新了我的连接字符串来解决问题
之后,我成功地从数据库中检索到了所需的信息。谢谢大家的帮助 在SQLServer配置管理器中进行了检查,我确实已将服务器名称更改为MAIN。此时,连接字符串如下所示:
但仍然没有结果。您使用什么名称从Visual Studio Server Explorer访问它?我使用的是./MAIN服务器,然后从下拉列表中选择我的示例
数据库。一切正常。在VS中,我可以轻松浏览表数据。我还用`base(“EmployeeContext”)创建了这个空构造函数。没有更改。您创建此构造函数是什么意思?你不是在用一个电话吗?当我需要先创建模型应用程序时,我喜欢使用这个工具,它节省了很多时间。在Sql Server Configuration manager中检查了它,我确实将服务器名称更改为MAIN。此时,连接字符串如下所示:
,但仍然没有结果