2012-11-27 188 views
0

即时使用.net webservice与本地数据库(mdf)。 这个所有的网站都运行良好,直到我将所有文件移动到另一台计算机上并创建了一个新的iis应用程序。与SQL Server连接到数据库mdf

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) 

香港专业教育学院改变web配置连接字符串几次,没有防火墙:在这个web服务工作得很好,只是方法需要连接和查询数据库,会出现以下错误 所有方法保护。

感谢...

+0

A'。mdf'文件是一个SQL Server数据库 - 你不能移动文件 - 你仍然需要在目标机器上安装SQL Server(Express或其他版本)... –

回答

1

我建议几个基础设施的检查,有时不仅仅是着眼于更好代码:

  1. 确保您的SQL服务器有适当的局域网连接
  2. 做一个ping从托管您的Web服务的服务器到托管您的SQL Server的服务器
  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"/> 

希望它能帮助,

+0

嗨豪尔赫,谢谢你的答案。即时通讯不使用SQL Server。它只是一个用linq查询本地数据库的asp应用程序。 –

+0

我明白了,遗憾的是,更新后的更好的可视性 –

+0

我的连接字符串:\t 节点 \t \t <添加名称=“gatewayConnectionString”的connectionString =“数据源= \ SQLEXPRESS;初始目录=网关;集成安全性= True“providerName =”System.Data.SqlClient“/> \t在其他计算机中它工作得很好...... maby它是一个iis问题? –

1

如果您使用的是MDF文件,必须有一个SQL Server或SQL Server Express上安装的原始机器。

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

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

+0

我刚刚在Visual Studio上创建了网站项目并将其附加到数据库。除了连接字符串,我需要更改哪些路径? –

+0

在我的其他电脑上,我只是从iis运行和配置。没有与SQL服务器定义。 –

+0

@DanielGabay:您**必须在该机器上安装** SQL Server Express。 –

1

MDF是标准的SQL Server的扩展,并且您正在使用的SqlClient连接,所以你肯定是使用SQL Server,但你连接到本地数据库(MDF)的文件,而不是实际的SQL Server服务。

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

  • 确保连接字符串已连接。至少在这里发布,这样我们可以提供帮助。

  • 要开始排除故障,暂时给每个人组访问该MDF文件,看看是否有帮助。

+0

我的连接字符串:在其他电脑它工作得很好......马比其iis问题? –

+0

此连接字符串连接到SQL Server,而不是MDF文件。它尝试连接到与Web服务器安装在同一服务器上的SQL Server,实例名称为SQLExpress。我猜测事实并非如此。您需要确定是要连接到SQL Server还是仅连接MDF文件。以下是连接到MDF文件的连接字符串示例:http://stackoverflow.com/questions/928813/connection-string-to-connect-to-mdf –