2016-03-06 162 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

相關問題