连接到没有SQL Server的数据库mdf

时间:2022-07-01 00:02:24

im using .net webservice with a LOCAL database (mdf). this all website worked fine until i moved all the files to another computer as is and created a new application on iis. all the methods on this webservice working just fine, just the methods that need to connect and query the database i get the following error:

即使用.net webservice和LOCAL数据库(mdf)。所有网站都运行良好,直到我将所有文件移动到另一台计算机,并在iis上创建一个新的应用程序。这个webservice上的所有方法都运行得很好,只需要连接和查询数据库的方法我得到以下错误:

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)

ive change the web config connection string couple of times and there is no firewall protection.

我已经多次更改Web配置连接字符串,并且没有防火墙保护。

thanks...

谢谢...

3 个解决方案

#1


1  

If you are using a MDF file, there must a SQL Server or SQL Server Express installed on your original machine.

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

As you say that you had moved your files to another computer, your configuration obviously do not work anymore. You will have to adjust all path information to fit to the new installation folders.

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

Also, you will have to make sure that there is a running SQL Server or SQL Server Express instance on the new computer AND make it point to the MDF database file.

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

#2


1  

MDF is the standard SQL Server extension, and you are using SqlClient to connect, so you are definitely using SQL Server, but you're connecting to a local database (MDF) file rather than an actual SQL Server Service.

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

  • Ensure that the account that your web application is running under has access to the MDF file.

    确保运行Web应用程序的帐户可以访问MDF文件。

  • Make absolutely certain the connection string is connect. At least post it here so we can help.

    确保连接字符串是连接的。至少在此发布,以便我们提供帮助。

  • To start troubleshooting, temporarily give the Everyone group access to the MDF file and see if that helps.

    要开始进行故障排除,请暂时让Everyone组访问MDF文件,看看是否有帮助。

#3


1  

I would suggest a couple of infrastructure checks, sometimes is better than just focusing on the code:

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

  1. Ensure your SQL server has proper LAN connection
  2. 确保您的SQL服务器具有正确的LAN连接
  3. Make a ping from the server hosting your web service to the the server hosting your SQL Server
  4. 从托管Web服务的服务器到托管SQL Server的服务器进行ping操作
  5. Use the MAnagement Studio to query for database objects from a different server (may be possible that a network administrator blocked important ports inadvertently)
  6. 使用MAnagement Studio从不同的服务器查询数据库对象(网络管理员可能会无意中阻止重要端口)

If possible check twice your connection string, sometimes a semicolon can ruin the whole thing.

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

EDIT

编辑

Connection String

连接字符串

<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"/> 

Hope it helps,

希望能帮助到你,

#1


1  

If you are using a MDF file, there must a SQL Server or SQL Server Express installed on your original machine.

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

As you say that you had moved your files to another computer, your configuration obviously do not work anymore. You will have to adjust all path information to fit to the new installation folders.

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

Also, you will have to make sure that there is a running SQL Server or SQL Server Express instance on the new computer AND make it point to the MDF database file.

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

#2


1  

MDF is the standard SQL Server extension, and you are using SqlClient to connect, so you are definitely using SQL Server, but you're connecting to a local database (MDF) file rather than an actual SQL Server Service.

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

  • Ensure that the account that your web application is running under has access to the MDF file.

    确保运行Web应用程序的帐户可以访问MDF文件。

  • Make absolutely certain the connection string is connect. At least post it here so we can help.

    确保连接字符串是连接的。至少在此发布,以便我们提供帮助。

  • To start troubleshooting, temporarily give the Everyone group access to the MDF file and see if that helps.

    要开始进行故障排除,请暂时让Everyone组访问MDF文件,看看是否有帮助。

#3


1  

I would suggest a couple of infrastructure checks, sometimes is better than just focusing on the code:

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

  1. Ensure your SQL server has proper LAN connection
  2. 确保您的SQL服务器具有正确的LAN连接
  3. Make a ping from the server hosting your web service to the the server hosting your SQL Server
  4. 从托管Web服务的服务器到托管SQL Server的服务器进行ping操作
  5. Use the MAnagement Studio to query for database objects from a different server (may be possible that a network administrator blocked important ports inadvertently)
  6. 使用MAnagement Studio从不同的服务器查询数据库对象(网络管理员可能会无意中阻止重要端口)

If possible check twice your connection string, sometimes a semicolon can ruin the whole thing.

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

EDIT

编辑

Connection String

连接字符串

<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"/> 

Hope it helps,

希望能帮助到你,