【问题标题】:How to solve Unable to connect to SQL Server Database?如何解决无法连接到 SQL Server 数据库?
【发布时间】:2015-12-30 19:21:05
【问题描述】:

如果我的问题是愚蠢的,或者就我是编程新手而言,请原谅我。这篇堆栈溢出文章 Visual Studio 2013 and ASP.NET Web Configuration Tool 为我节省了运行 Visual Studio 2013 WSA 工具的时间,但我遇到了一个新问题,那就是与 SQL Server 数据库的连接。

问题是当我通过 cmd 运行 IIS Express,然后将地址输入浏览器时,我被重定向到 WSA 工具,但是当我点击安全选项卡或链接时,我收到此错误:

您选择的数据存储存在问题。这可能是由无效的服务器名称或凭据或权限不足引起的。这也可能是由于未启用角色管理器功能造成的。单击下面的按钮可重定向到您可以选择新数据存储的页面。

以下消息可能有助于诊断问题:

无法连接到 SQL Server 数据库

我的连接字符串如下所示:

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=.; Initial Catalog=aspnetdb; User Id=sa; Password=***********; Integrated Security=true" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

我的整个应用程序web.config 如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.; Initial Catalog=aspnetdb; User Id=sa; Password=***********; Integrated Security=true" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
  <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers></system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

我在我的 MVC 应用程序中看不到 &lt;rolemanager enabled=true&gt;,即使我尝试了一个具有此 &lt;rolemanager enabled="true"&gt; 的 Web 应用程序但甚至没有工作。

P.S:值得一提的是,当我自己运行应用程序时,它可以正常工作,我可以创建用户、更新用户密码等……结果也会反映到 MS SQL Server 数据库表中,但是不知道这里有什么魔力。

你有什么建议?!

问候 道斯达

【问题讨论】:

标签: c# asp.net asp.net-mvc-4 sql-server-2012


【解决方案1】:

如果您打算以 sa 身份连接到 SQL Server,请尝试从连接字符串中删除“Integrated Security=true”。

当它设置为 true 时,当前的 Windows 帐户凭据用于身份验证。您的 Windows 用户可能对数据库没有适当的权限。

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx

【讨论】:

  • 我改变了它,但它甚至没有工作。我在安装 MS SQL Server 期间添加了我当前的 Windows 用户 ID。
【解决方案2】:

我认为您需要转到 sql server 并在对象资源管理器中搜索安全 -> sa -> 状态 -> 登录 -> 启用

【讨论】:

    【解决方案3】:

    查看下面的连接字符串,您指向的是本地完整的 SQL Server 安装实例,但那里真的有一个名为 aspnetdb 的数据库吗?因为您在连接字符串Initial Catalog=aspnetdb; 中提到了这一点。

    您尝试访问的 aspnetdb 是一个 ASP.NET 身份数据库,我强烈怀疑它是您的解决方案/项目中 App_Data 文件夹下的附加数据库。在这种情况下,您的连接字符串是错误的(您应该指向localDB 而不是),因此您将面临错误。

    <connectionStrings>
        <add name="DefaultConnection" 
             connectionString="Data Source=.; Initial Catalog=aspnetdb; ...
             providerName="System.Data.SqlClient" />
    </connectionStrings>
    

    【讨论】:

    • 是的,我确实有名为 aspnetdb 的 sql db,我的 App_Data 中没有任何附加数据库
    【解决方案4】:

    试试这个。

    更改web.config连接字符串

    <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=Your PC Name; Initial Catalog=aspnetdb; User Id=sa; Password=***********; " providerName="System.Data.SqlClient" />
    </connectionStrings>
    

    例如。数据源=XYZ\SQLEXPRESS 并尝试删除 Integrated Security=true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多