我无法从 C# 连接到特定的 SQL Server 2008 数据库服务器。
我能够使用 SSMS 进行连接,并使用 SQLCMD 运行查询,但是当我尝试使用 SqlConnection 从 C# 进行连接时,它无法打开与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)
这是我的代码:
using (SqlConnection conn = new SqlConnection(@"Server=LDNPSM050000137\PLN000000014T;Initial Catalog=MiscData;Integrated Security=True;"))
{
//exception occurs on this line
conn.Open();
//use connection
conn.Close();
}
我使用 ODBC 得到类似的响应:
string connectionString = @"Driver={SQL Server Native Client 10.0};Server=LDNPSM050000137\PLN000000014T;Database=MiscData;Trusted_Connection=yes;";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
connection.Open();
}
此操作因 OdbcExcpetion 而失败:
ERROR [08001] [Microsoft][SQL Server Native Client 10.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
ERROR [HYT00] [Microsoft][SQL Server Native Client 10.0]Login timeout expired
ERROR [08001] [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
我检查了服务器和实例名称是否正确,并且服务器配置为允许远程连接,因为我可以通过 SSMS 连接。有没有人建议问题可能是什么或如何解决这个问题?
更新:
如果这有助于某人诊断问题 - 这特定于我的机器/用户帐户。我的同事可以从他的机器上正常运行代码。我还可以使用对 Server 和 Initial Catalog 进行适当更改的连接字符串连接到我的本地实例。
所有远程数据库服务器都出现错误。
请您参考如下方法:
我不确定这是否特定于我公司的特定 IT 基础架构 - 但是这对我不起作用的原因是因为该项目保存在存储在网络共享上的我的文档中。显然,当执行代码位于网络共享上时,.NET 不会让您连接到远程数据库服务器 - 当我将项目复制到本地驱动器时它工作正常。我正在使用 Windows 7 和 Visual Studio 2012。
