繁体   English   中英

如何解决无法连接到SQL Server数据库?

[英]How to solve Unable to connect to SQL Server Database?

请原谅我,如果我的问题是愚蠢的,或者只要我是编程的新手。 这个堆栈溢流文章Visual Studio 2013和ASP.NET Web配置工具为运行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应用程序中看不到<rolemanager enabled=true> ,即使我尝试了一个已经具有此<rolemanager enabled="true">但是甚至无法工作的Web应用程序。

PS:值得一提的是,当我运行应用程序时,它自己运行得更好,我可以创建用户,更新用户密码等...并且结果也反映回MS SQL Server数据库表但是没有知道这里有什么魔力。

你有什么建议的?!

问候Dostdar

如果您打算以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

我想你需要去sql server并在对象资源管理器搜索安全 - > sa - > status - > login - > enabled

查看下面的连接字符串,您指向一个本地完整的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>

试试这个。

更改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

暂无
暂无

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

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