简体   繁体   English

如何使用 Visual Studio 通过 C# 连接到 SQL 服务器实例

[英]How to connect to a SQL Server instance with C# using Visual Studio

I am unable to connect to SQL Servers that have the '\' char which is used when connecting to a server instance.我无法连接到具有连接到服务器实例时使用的“\”字符的 SQL 服务器。 Any other type of server works without any issues.任何其他类型的服务器都可以正常工作。 Any support or resource on this issue would be extremely helpful as I have exhausted my possible solutions.由于我已经用尽了可能的解决方案,因此有关此问题的任何支持或资源都将非常有帮助。

Thanks!谢谢! Chris克里斯

I have tried: - adding/removing the '@' - using a double '\' - hard coding the server name and connecting我试过: - 添加/删除'@' - 使用双'\' - 硬编码服务器名称并连接

var masterConnString = $@"Server={Server}; Database={ProjectCode}; Trusted_Connection=True;";

using (var connection = new SqlConnection(masterConnString))
{                
     connection.Open();
     //do something
     connection.Close();
}

The following error occurs:出现以下错误:

System.Data.SqlClient.SqlException HResult=0x80131904 Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. System.Data.SqlClient.SqlException HResult=0x80131904 消息=与 SQL 服务器建立连接时发生网络相关或特定于实例的错误。 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.验证实例名称是否正确,并且 SQL 服务器配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) Source=.Net SqlClient Data Provider (提供者:命名管道提供者,错误:40 - 无法打开与 SQL 服务器的连接)Source=.Net SqlClient 数据提供者

Update:更新:

When attempting to update the SQL Server Configuration Manager the connection fails due to lack of permissions.尝试更新 SQL 服务器配置管理器时,由于缺少权限,连接失败。

Remove the trusted param and add this param删除受信任的参数并添加此参数

"Integrated Security=SSPI" “集成安全 = SSPI”

This always works with me when the server don't have any authentication当服务器没有任何身份验证时,这总是适用于我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Visual Studio 2019 中从 C# 连接到 SQL 服务器 - How to connect to SQL Server from C# in Visual Studio 2019 如何使用Visual C#连接到远程SQL Server数据库? - How to connect to remote SQL Server database using Visual C#? 使用C#连接到SQL Server 2012数据库(Visual Studio 2012) - Connect to SQL Server 2012 Database with C# (Visual Studio 2012) 在尝试使用C#连接到Visual Studio 2010中的本地SQL Server数据库时遇到问题 - Having problems trying to connect to local SQL Server database in Visual Studio 2010 using C# Selenium webdriver - 如何连接到由 Visual Studio 使用 C# 打开的同一个 firefox 实例 - Selenium webdriver - How to connect to the same firefox instance open by Visual Studio using C# 如何使用Visual Studio 2017中编译的C#应用​​程序连接到SQL Server 2008? - How to connect to SQL Server 2008 with C# application compiled in Visual Studio 2017? 如何将Visual Studio 2010 Express C#连接到SQL Server Express - How to connect Visual Studio 2010 Express C# to SQL Server Express 如何在Visual Studio(C#)中将ASP.NET项目与SQL Server数据库表连接 - How to connect ASP.NET project with SQL Server database table in Visual Studio (C#) 如何使用Visual Studio(C#)通过Internet连接到ArcGIS Server - How to connect to ArcGIS Server over the Internet using Visual Studio (C#) 如何将在 Visual Studio C# 中创建的数据库迁移到 SQL 服务器? - How to migrate a DataBase created in Visual Studio C# to a SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM