将ASP.net网站部署到IIS7并使用LocalDB。

3
首先,我要道歉,因为我发现许多类似的问题,但没有一个能解决我的问题。
我在Visual Studio 2012 Ultimate中开发了一个ASP.net网站(C#)。我创建了App_Data文件夹并创建了一个SQL Sever数据库。当所有开发工作完成后,我决定将网站部署到Windows Server 2008 R2上的IIS7。部署后,大多数网页都可以正常工作,除了那些需要从数据库获取数据的页面。
我在IIS中选择了.Net Framework版本为v4.0
这是我在访问带有gridview的页面时收到的错误信息。
Server Error in '/' Application.

The system cannot find the file specified 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ComponentModel.Win32Exception: The system cannot find the file specified

Source Error: 

 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:

[Win32Exception (0x80004005): The system cannot find the file specified]
[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: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)]
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +414
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +196
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +146
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +16
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +94
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +110
   System.Data.SqlClient.SqlConnection.Open() +96
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +121
   System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +160
   System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +108
   dsNotificaTableAdapters.pessoasEnvolvidasTableAdapter.getPessoasEnvolvidas() +101
   notificar.fillrblPessoas() +41
   notificar.Page_Load(Object sender, EventArgs e) +327
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446 

Thanks for your help in advance!


你是如何连接到数据库的?它在哪里?你发布的站点是否正确指向它?这个错误听起来像是已发布的站点找不到你的数据库... - Dylan Corriveau
嗨@DylanCorriveau,数据库位于App_Data文件夹内的网站文件夹中。 webconfig文件具有以下内容: <connectionStrings> <add name="notificaConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\notifica.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> - Sota
3个回答

2

[SqlException (0x80131904): 在建立到 SQL Server 的连接时发生与网络或特定实例相关的错误。无法找到服务器或无法访问。请验证实例名称是否正确,并确认 SQL Server 配置为允许远程连接。(提供程序: SQL Network Interfaces,错误: 52 - 无法找到本地数据库运行时安装。请验证已正确安装 SQL Server Express 并启用了本地数据库运行时功能。)]

LocalDB (App_Data 内部的 SQL 数据库) 主要用于开发和测试目的。当您发布到生产环境时,它将不会随源代码一起复制。

当应用程序上线时,您需要一个真正的 SQL Server。因此,您需要在托管应用程序的同一服务器或专用 SQL 服务器上安装 SQL Server,然后附加该数据库。


嗨@Win,我在我的Windows Server 2008 R2上安装了SQL Server 2014 Express。我使用sqlcmd命令行附加了我的数据库。 SQL Server、SQL Server Browser和SQL Server VSS Writer服务正在运行。你能否提供进一步的帮助来完成这个任务?谢谢。 - Sota
确保您可以使用SSMS手动连接到SQL Server。如果可以,那么请更新应用程序的Web.config中的连接字符串。如果您无法使用SSMS连接到SQL Server,则需要解决问题(关于此问题已经在SO上提出了很多问题)。 - Win
我看不到提供的错误消息描述了您所编写的内容(例如,“App_Data 中的 SQL 数据库仅用于开发/测试目的。”)。它只是说找不到 LocalDB 文件。 - Alex G.P.
@AlexG.P. 我只是想解释一下,LocalDB 主要是用于开发和测试的目的。当您发布到生产环境时,它不会随源代码一起复制。理想情况下,不应在生产环境中使用它。如果您有问题,请创建一个新问题;我很乐意帮助您。 - Win

1

要使LocalDB正常工作,仍需在计算机上安装它,它并不是SQL Server的免费版本,不能自动包含在应用程序部署中。在您的本地工作站上,安装Visual Studio时也会安装LocalDB。

因此,最终您仍需要一个数据库服务器,以便运行应用程序的服务器可以连接到它。LocalDB旨在供开发人员在本地使用,因此在服务器情况下,您可能需要考虑使用SQL Server Express。


0

您需要在应用程序池配置中设置一个值,以使您的站点能够访问您的 .mdf 文件:

在 C:\Windows\System32\inetsrv\config\applicationHost.config 中

设置 etProfileEnvironment=“true“,如下所示:

<add name=“ASP.NET v4.0“ autoStart=“true“ managedRuntimeVersion=“v4.0“ managedPipelineMode=“Integrated“>
<processModel identityType=“ApplicationPoolIdentity“ loadUserProfile=“true“ setProfileEnvironment=“true“ />
</add>

https://blogs.msdn.microsoft.com/sqlexpress/2011/12/08/using-localdb-with-full-iis-part-1-user-profile/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接