簡體   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