简体   繁体   English

C#ASP.NET连接到数据库错误

[英]C# ASP.NET connection to Database error

So, i am trying to learn ASP.NET but i am stuck when it comes to DB connections. 因此,我正在尝试学习ASP.NET,但在涉及数据库连接时却陷入困境。

I get the following error: 我收到以下错误:

Unable to find the requested .Net Framework Data Provider. 找不到请求的.Net Framework数据提供程序。 It may not be installed. 它可能没有安装。

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.ArgumentException: Unable to find the requested .Net Framework Data Provider. 异常详细信息:System.ArgumentException:无法找到请求的.Net Framework数据提供程序。 It may not be installed. 它可能没有安装。

and

Stack Trace: 堆栈跟踪:

[ArgumentException: Unable to find the requested .Net Framework Data Provider. [ArgumentException:无法找到请求的.Net Framework数据提供程序。 It may not be installed.] 它可能没有安装。]
System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName) +1480903 System.Data.Common.DbProviderFactories.GetFactory(字符串providerInvariantName)+1480903
WebMatrix.Data.DbProviderFactoryWrapper.CreateConnection(String connectionString) +63 WebMatrix.Data.DbProviderFactoryWrapper.CreateConnection(String connectionString)+63
WebMatrix.Data.<>c__DisplayClass15.b__14() +16 WebMatrix.Data。<> c__DisplayClass15.b__14()+16
WebMatrix.Data.Database.get_Connection() +19 WebMatrix.Data.Database.get_Connection()+19
WebMatrix.Data.Database.EnsureConnectionOpen() +12 WebMatrix.Data.Database.EnsureConnectionOpen()+12
WebMatrix.Data.d__0.MoveNext() +66 WebMatrix.Data.d__0.MoveNext()+66
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)+381
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58 System.Linq.Enumerable.ToList(IEnumerable`1源)+58
WebMatrix.Data.Database.Query(String commandText, Object[] parameters) +102 WebMatrix.Data.Database.Query(字符串commandText,Object []参数)+102
ASP._Page_NewUser_cshtml.Execute() in c:\\Users\\Euaggelos\\Documents\\Visual Studio 2013\\WebSites\\WebSite5\\NewUser.cshtml:20 c:\\ Users \\ Euaggelos \\ Documents \\ Visual Studio 2013 \\ WebSites \\ WebSite5 \\ NewUser.cshtml:20中的ASP._Page_NewUser_cshtml.Execute()
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +199 System.Web.WebPages.WebPageBase.ExecutePageHierarchy()+199
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69 System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1执行者)+69
System.Web.WebPages.WebPage.ExecutePageHierarchy() +131 System.Web.WebPages.WebPage.ExecutePageHierarchy()+131
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage)+78
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +116 System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext)+116

The problem is that i DO have the .net Framework Data Provider installed. 问题是我确实安装了.net Framework数据提供程序。

Here are parts of my code: 这是我的代码的一部分:

<connectionStrings>
    <add name="DB1Entities" connectionString="metadata=res://*/App_Code.Model.csdl|res://*/App_Code.Model.ssdl|res://*/App_Code.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\DB1Entities.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

@using WebMatrix.Data;

@{
    Page.Title = "Add you user";
    Layout = "_Layout.cshtml";
    var db = Database.Open("DB1Entities"); 
    var selectQueryString = "SELECT * FROM Users ORDER BY FirstName";
}



<h1>New user</h1>

<table>
    <tr>
        <th>Id</th>
        <th>First name</th>
        <th>Last name</th>
    </tr>
    @foreach (var row in db.Query(selectQueryString)) <!--THIS IS THE ERROR LINE-->
    {
        <tr>
            <td>@row.Id</td>
            <td>@row.FirstName</td>
            <td>@row.LastName</td>
        </tr>
    }
</table> 

在此处输入图片说明

You might need to add this to your app.config file 您可能需要将此添加到您的app.config文件中

  <system.data>
<DbProviderFactories>
  <remove invariant="System.Data.SqlServerCe.4.0" />
  <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>

Your connection string is for an Entity Framework Database First project. 您的连接字符串用于Entity Framework Database First项目。 However, you don't appear to be using EF in your WebMatrix site. 但是,您似乎未在WebMatrix网站中使用EF。 You are using standard SQL to query the database. 您正在使用标准SQL查询数据库。 Change the connection string to a plain one: 将连接字符串更改为普通字符串:

<connectionStrings>
    <add name="DB1Entities" connectionString="data source=LocalDB)\v11.0;attachdbfilename=|DataDirectory|\DB1Entities.mdf;integrated security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

If you want to use Entity Framework with a Web Pages project, you should use the Code First approach as detailed in my article here: http://www.mikesdotnetting.com/article/182/entity-framework-code-first-development-with-webmatrix 如果要将Entity Framework与Web Pages项目一起使用,则应使用我在本文中详细介绍的Code First方法: http : //www.mikesdotnetting.com/article/182/entity-framework-code-first-development -with-webmatrix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM