【问题标题】:Connecting to Database mdf without SQL Server在没有 SQL Server 的情况下连接到数据库 mdf
【发布时间】:2012-11-27 12:02:00
【问题描述】:

我使用带有本地数据库 (mdf) 的 .net 网络服务。 在我将所有文件按原样移动到另一台计算机并创建之前,这个所有网站都运行良好 iis 上的新应用程序。 这个网络服务上的所有方法都工作得很好,只是需要连接和查询数据库的方法我得到以下错误:

System.Data.SqlClient.SqlException: 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: 26 - Error Locating Server/Instance Specified)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user)
   at System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe()
   at System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode()
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.ElementAt[TSource](IQueryable`1 source, Int32 index)

我多次更改网络配置连接字符串,但没有防火墙保护。

谢谢...

【问题讨论】:

  • .mdf 文件是 SQL Server 数据库 - 您不能只移动文件 - 您仍然需要在目标计算机上安装 SQL Server(Express 或其他版本)...

标签: .net sql-server database


【解决方案1】:

如果您使用的是 MDF 文件,则必须在您的原始计算机上安装 SQL Server 或 SQL Server Express。

正如您所说,您已将文件移动到另一台计算机上,您的配置显然不再有效。您必须调整所有路径信息以适应新的安装文件夹。

此外,您必须确保新计算机上有一个正在运行的 SQL Server 或 SQL Server Express 实例,并使其指向 MDF 数据库文件。

【讨论】:

  • 我刚刚在 Visual Studio 上创建了网站项目并将其附加到数据库中。除了连接字符串,我需要更改哪些路径?
  • 在我的另一台计算机上,我只是从 iis 运行和配置。没有 sql server 定义。
  • @DanielGabay:您必须在该计算机上安装 SQL Server Express。
  • 根据我的需要,我需要安装什么版本?
  • 您需要与另一台机器上相同的版本。
【解决方案2】:

MDF 是标准的 SQL Server 扩展,并且您使用 SqlClient 进行连接,因此您肯定使用的是 SQL Server,但您连接的是本地数据库 (MDF) 文件,而不是实际的 SQL Server 服务。

  • 确保运行 Web 应用程序的帐户有权访问 MDF 文件。

  • 绝对确定连接字符串已连接。至少在此处发布,以便我们提供帮助。

  • 要开始故障排除,暂时授予每个人组访问 MDF 文件的权限,看看是否有帮助。

【讨论】:

  • 我的连接字符串: 在其他计算机上它工作得很好......可能是 iis 问题?
  • 此连接字符串连接到 SQL Server,而不是 MDF 文件。它尝试连接到安装在与您的 Web 服务器相同的服务器上的 SQL Server,实例名称为 SQLExpress。我猜情况并非如此。您需要确定是要连接到 SQL Server 还是仅连接到 MDF 文件。以下是连接到 MDF 文件的连接字符串示例:stackoverflow.com/questions/928813/…
【解决方案3】:

我建议进行一些基础设施检查,有时比只关注代码更好:

  1. 确保您的 SQL 服务器具有正确的 LAN 连接
  2. 从托管 Web 服务的服务器向托管 SQL Server 的服务器执行 ping 操作
  3. 使用 MAnagement Studio 从其他服务器查询数据库对象(可能是网络管理员无意中阻止了重要端口)

如果可能的话,检查两次你的连接字符串,有时分号会毁掉整个事情。

编辑

连接字符串

<add name="gatewayConnectionString" 
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\Projects\p\file.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" 
providerName="System.Data.SqlClient"/> 

希望对你有帮助,

【讨论】:

  • 嗨豪尔赫,感谢您的回答。我没有使用 sql server。它只是一个使用 linq 查询本地数据库的 asp 应用程序。
  • 我明白了,很抱歉,更新了帖子以提高知名度
  • 我的连接字符串: 在其他计算机上它工作得很好......可能是 iis 问题?
  • 你使用了错误的连接字符串,看看我的例子 AttachedDBFileName
  • @Daniel 在我看来,您没有设置数据库的文件路径,当您将数据库更改为另一台计算机时,您的文件系统结构肯定发生了变化,这就是您需要更新的原因它,你能再次发布你的更新的连接字符串吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多