Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL连接问题_C#_Asp.net_Sql Server - Fatal编程技术网

C# SQL连接问题

C# SQL连接问题,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我使用Visual Studio 2012的内置asp.net网站模板开发了一个网站 它在ASP.NET开发服务器上运行良好 当我在IIS上上传网站时。我在尝试登录时遇到了下面提到的错误 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到服务器,或者 无法访问。验证实例名称是否正确,以及 SQL Server配置为允许远程连接。(提供程序:SQL) 网络接口,错误:50-发生本地数据库运行时错误。 无法创建自动实例。请参阅Windows应用程序事件 记录错误详细信息 堆栈

我使用Visual Studio 2012的内置asp.net网站模板开发了一个网站

它在ASP.NET开发服务器上运行良好

当我在IIS上上传网站时。我在尝试登录时遇到了下面提到的错误

建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到服务器,或者 无法访问。验证实例名称是否正确,以及 SQL Server配置为允许远程连接。(提供程序:SQL) 网络接口,错误:50-发生本地数据库运行时错误。 无法创建自动实例。请参阅Windows应用程序事件 记录错误详细信息

堆栈跟踪

[SqlException (0x80131904): 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: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
)]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +6675286
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +688
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +6701832
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +219
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +6703968
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +6704427
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +610
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +1049
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +74
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions) +6706995
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions) +78
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +2192
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +116
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1012
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +6711619
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +152
   System.Data.SqlClient.SqlConnection.Open() +229
   System.Data.SqlClient.SqlProviderServices.UsingConnection(SqlConnection sqlConnection, Action`1 act) +134
   System.Data.SqlClient.SqlProviderServices.UsingMasterConnection(SqlConnection sqlConnection, Action`1 act) +3790907
   System.Data.SqlClient.SqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +10941050
   System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +91

[ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.]
   System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +10941121
   System.Web.Providers.ModelHelper.CreateMetadataWorkspace(String providerName, DbConnection connection, String csdl, String ssdl, String msl) +168
   System.Web.Providers.ModelHelper.CreateEntityConnection(ConnectionStringSettings setting, String csdl, String ssdl, String msl) +298
   System.Web.Providers.ModelHelper.CreateMembershipEntities(ConnectionStringSettings setting) +51
   System.Web.Providers.DefaultRoleProvider.IsUserInRole(String username, String roleName) +180
   System.Web.Security.Roles.IsUserInRole(String username, String roleName) +794
   LoginPage.Login1_Authenticate(Object sender, AuthenticateEventArgs e) in c:\inetpub\wwwroot\TimeCard\LoginPage.aspx.cs:19
   System.Web.UI.WebControls.Login.AttemptLogin() +160
   System.Web.UI.WebControls.Login.OnBubbleEvent(Object source, EventArgs e) +93
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +84
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804
我的网络配置文件是

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-TimeCard-20131218110938;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-TimeCard-20131218110938.mdf"
      providerName="System.Data.SqlClient" />
    <add name="TimeCardConnectionString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TimeCard;Integrated Security=SSPI"
      providerName="System.Data.SqlClient" />
    <add name="MCASConnectionString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="TimeCardConnectionString2" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TimeCard;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <authorization>
      <allow roles="Admin" />
    </authorization>
    <compilation debug="true" >
      <assemblies>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation >

    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization"/>
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
      </controls>
    </pages>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/"/>
    </authentication>
    <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/"/>
      </providers>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="DefaultRoleProvider">
      <providers>
        <add connectionStringName="DefaultConnection" applicationName="/"
          name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </providers>
    </roleManager>
    <!--
            If you are deploying to a cloud environment that has multiple web server instances,
            you should change session state mode from "InProc" to "Custom". In addition,
            change the connection string named "DefaultConnection" to connect to an instance
            of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
      -->
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection"/>
      </providers>
    </sessionState>
  </system.web>
  <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>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
  </entityFramework>
</configuration>


应该是什么问题?

您正在连接字符串中为数据库使用
localhost\SQLEXPRESS
——如果这是一台服务器,则需要将其替换为实时服务器的DB实例的名称

这是您的配置部分

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-TimeCard-20131218110938;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-TimeCard-20131218110938.mdf"
      providerName="System.Data.SqlClient" />
    <add name="TimeCardConnectionString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TimeCard;Integrated Security=SSPI"
      providerName="System.Data.SqlClient" />
    <add name="MCASConnectionString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="TimeCardConnectionString2" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TimeCard;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

指向服务器正在使用的数据库


我假设您也需要更新凭据。

您正在连接字符串中为您的数据库使用
localhost\SQLEXPRESS
——如果这是一台服务器,则需要将其替换为实时服务器的DB实例的名称

这是您的配置部分

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-TimeCard-20131218110938;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-TimeCard-20131218110938.mdf"
      providerName="System.Data.SqlClient" />
    <add name="TimeCardConnectionString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TimeCard;Integrated Security=SSPI"
      providerName="System.Data.SqlClient" />
    <add name="MCASConnectionString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="TimeCardConnectionString2" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TimeCard;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

指向服务器正在使用的数据库

我想您也需要更新凭据。

这一个

connectionString="Data Source=.\SQLEXPRESS;


这个

connectionString="Data Source=.\SQLEXPRESS;


运行时发生与网络相关或特定于实例的错误 正在建立与SQL Server的连接。找不到该服务器,或者 无法访问。请验证实例名称是否正确,以及 SQL Server配置为允许远程连接。(提供程序:SQL) 网络接口,错误:50-发生本地数据库运行时错误。 无法创建自动实例。请参阅Windows应用程序事件 记录错误详细信息

在完全IIS下运行时,LocalDB有两个属性会导致问题:

1.LocalDB需要加载用户配置文件

2.LocalDB实例由单个用户拥有,并且是私有的(默认情况下)

很可能未加载用户配置文件。如果在IIS下执行LocalDB,请确保为当前用户启用了配置文件加载

搜索谷歌,发现了这篇很棒的帖子:

当我将ASP.NET MVC 4应用程序部署到本地IIS中的一个新网站并创建了一个新的应用程序池时,我也遇到了这个问题。 正如上文所述,必须为文件中的AppPool启用setProfileEnvironment=true

 C:\Windows\System32\inetsrv\config\applicationHost.config.
所以加上这个

<add name="MyAppPoolName" />
<processModel identityType="ApplicationPoolIdentity"
              loadUserProfile="true"
              setProfileEnvironment="true" />
</add>

运行时发生与网络相关或特定于实例的错误 正在建立与SQL Server的连接。找不到该服务器,或者 无法访问。请验证实例名称是否正确,以及 SQL Server配置为允许远程连接。(提供程序:SQL) 网络接口,错误:50-发生本地数据库运行时错误。 无法创建自动实例。请参阅Windows应用程序事件 记录错误详细信息

在完全IIS下运行时,LocalDB有两个属性会导致问题:

1.LocalDB需要加载用户配置文件

2.LocalDB实例由单个用户拥有,并且是私有的(默认情况下)

很可能未加载用户配置文件。如果在IIS下执行LocalDB,请确保为当前用户启用了配置文件加载

搜索谷歌,发现了这篇很棒的帖子:

当我将ASP.NET MVC 4应用程序部署到本地IIS中的一个新网站并创建了一个新的应用程序池时,我也遇到了这个问题。 正如上文所述,必须为文件中的AppPool启用setProfileEnvironment=true

 C:\Windows\System32\inetsrv\config\applicationHost.config.
所以加上这个

<add name="MyAppPoolName" />
<processModel identityType="ApplicationPoolIdentity"
              loadUserProfile="true"
              setProfileEnvironment="true" />
</add>

这个问题很常见,请检查我答案中的链接,然后执行几个步骤,如果在这之后出现错误,请让我知道。!!这个问题很常见,请检查我答案中的链接,然后执行几个步骤,如果在这之后出现错误,请让我知道由于我只是软件开发的初学者,所以我甚至不能将mdf文件附加到管理层studio@SPandya我已经尝试过扩展,我不确定我还可以添加什么-您可以联系您的主机提供商的支持团队,询问有关连接和部署数据库的信息吗?数据库位于解决方案的负责人哪一个?您的配置文件中设置了4个数据库,其中至少有一个没有连接,没有您的代码,我们无法分辨哪个数据库。就我个人而言,我不会弄乱MDF文件,我编写数据库脚本并以这种方式部署它。另一张海报提供了出色的诊断步骤,您的主机提供商知道环境…我们我恐怕是瞎了眼!你能给我举个例子吗?因为我只是软件开发的初学者。问题是我甚至不能将mdf文件附加到管理层studio@SPandya我已经尝试过扩展,我不确定我还可以添加什么-您可以联系您的主机提供商的支持团队,询问有关连接和部署dat的问题吗abase?数据库在解决方案的App_Data文件夹中哪一个?您的配置文件中设置了4个数据库,其中至少有一个没有连接,没有您的代码,我们无法分辨哪个数据库。我个人不会弄乱MDF文件,我记下