2016-07-10 15 views
0

Ich arbeite an diesem asp.net Webservice mit MVC5, C#, um Produkte zu verwalten, unter Windows 10, mit Visual Studio 2015 Community.Warum SQL-Ausnahme?

Als ich die Datenbankverbindung hinzufügte, und lief es von index.shtml, um die Produkte/Indexseite zu sehen, aber Visual Studio gab mir diesen Fehler.

SQL Exception was unhandled by user code. An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

ich ein Anfänger bin, damit ich so viel wie ich kann recherchiert, und ich weiß nicht meine Fehler sehen, wie kann ich es beheben?

EDIT: Könnte der Fehler sein, dass ich phpMyAdmin nicht für MySQL installiert habe?

II BEARBEITEN: Ich habe gerade festgestellt, dass diese neue Maschine nicht meine alte Konfiguration hat, ich werde MySQL installieren und Bericht erstatten. Vielen Dank.

Danke!

Das ist meine Produktklasse (Modell):

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace MVCStart.Models 
{ 
    public class Product 
    { 
     [Key] 
     public int ID { get; set; } 
     public string Description { get; set; } 
     public decimal Price { get; set; } 
     public DateTime LastBuy { get; set; } 
     public float Stock { get; set; } 
    } 
} 

Das ist mein Controller ist:

using MVCStart.Context; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace MVCStart.Controllers 
{ 
    public class ProductController : Controller 
    { 
     private StoreContext db = new StoreContext(); 

     // GET: Product 
     public ActionResult Index() 
     { 
      return View(db.Products.ToList()); 
     } 

     // GET: Product/Details/5 
     public ActionResult Details(int id) 
     { 
      return View(); 
     } 

     // GET: Product/Create 
     public ActionResult Create() 
     { 
      return View(); 
     } 

     // POST: Product/Create 
     [HttpPost] 
     public ActionResult Create(FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add insert logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

     // GET: Product/Edit/5 
     public ActionResult Edit(int id) 
     { 
      return View(); 
     } 

     // POST: Product/Edit/5 
     [HttpPost] 
     public ActionResult Edit(int id, FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add update logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

     // GET: Product/Delete/5 
     public ActionResult Delete(int id) 
     { 
      return View(); 
     } 

     // POST: Product/Delete/5 
     [HttpPost] 
     public ActionResult Delete(int id, FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add delete logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 
    } 
} 

Das ist mein StoreContext für Datenbank:

using MVCStart.Models; 
using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 

namespace MVCStart.Context 
{ 
    public class StoreContext: DbContext 
    { 
     public DbSet<Product> Products { get; set; } 
    } 
} 

Dies ist meine Ansicht mit html:

@model IEnumerable<MVCStart.Models.Product> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Description) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Price) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.LastBuy) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Stock) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Description) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Price) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.LastBuy) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Stock) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | 
      @Html.ActionLink("Details", "Details", new { id=item.ID }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.ID }) 
     </td> 
    </tr> 
} 

</table> 

Das ist mein webconfig mit Datenbankeinstellungen:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=301880 
    --> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <add name="StoreContext" 
     connectionString="Data Source=.;Initial Catalog=Market;Integrated Security=True" 
     providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <authentication mode="None" /> 
    <compilation debug="true" targetFramework="4.5.2" /> 
    <httpRuntime targetFramework="4.5.2" /> 
    </system.web> 
    <system.webServer> 
    <modules> 
     <remove name="FormsAuthentication" /> 
    </modules> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
</configuration> 
+1

Haben Sie einen SQL-Server auf Ihrem lokalen Computer und ist er eingeschaltet? Es versucht, sich mit ihm zu verbinden und Zeit zu verlieren. Datenquelle =. bedeutet localhost. Wenn Sie versuchen, sich mit MySQL zu verbinden, müssen Sie die Verbindungszeichenfolge in Ihrer web.config ändern. –

+0

Nein, ich habe gerade festgestellt, dass ich gerade auf eine neue Maschine gewechselt bin und diese hat meine alte Konfiguration leider nicht. Ich werde es installieren und zurück melden. Vielen Dank. – 4201

+0

Ihre Web.Config ist für Sql Server konfiguriert, nicht MySql. Sie können dies auch sehen, indem Sie die Ausnahme lesen, die Sie bekommen: 'Konnte keine Verbindung zu SQL Server öffnen' – Kinetic

Antwort

0

Open Server Explorer von VS über View-> Server-Explorer.
Öffnen Sie die Datenverbindungen und löschen Sie die zugehörige Verbindung (StoreContext).
Fügen Sie dann eine neue Verbindung hinzu, indem Sie mit der rechten Maustaste auf Datenverbindungen klicken.