如何在另一台计算机上为Microsoft SQL Server制作连接字符串
我不知道如何制作连接字符串以访问另一台计算机上的数据库。
我正在使用Visual Studio 2022,当我从服务器资源管理器复制连接字符串时,它指向我的计算机上的.mdf
文件,因此显然只能在我的主机机器上使用(那是我的电脑)。
这是连接字符串:
conn = new SqlConnection("Data Source=DESKTOP-42N6RDI; Initial Catalog=Database1");
我相信我不需要指定用户ID和密码。
我已经尝试添加端口,并且也尝试添加IP而不是服务器名称,但它不起作用。
我遇到的错误总是相同的:
类型“ system.data.sqlclient.sqlexception”的未经处理的例外发生在system.data.dll
中 在建立与SQL Server的连接时发生了与网络有关的或特定实例的错误。该服务器未找到或无法访问。验证实例名称是正确的,并且配置了SQL Server以允许远程连接。 (提供商:命名Pipes提供商,错误:40-无法打开与SQL Server的连接)
编辑:它也表示“内部异常”
win32 exception:系统找不到指定的文件
。
I can't figure out how to make a connection string to access a database on another machine.
I'm using Visual Studio 2022 and when I copy the connection string from the server explorer it's pointing to the .mdf
file on my computer, so that's clearly only going to work on my host machine (that is my computer).
Here's the connection string:
conn = new SqlConnection("Data Source=DESKTOP-42N6RDI; Initial Catalog=Database1");
I believe I don't need to specify the user id and the password.
I've tried to add the port, and I've also tried to add the ip instead of the server name but it just doesn't work.
The error I'm getting is always the same:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
EDIT: It also says "Inner Exception"
Win32Exception: The system cannot find the file specified
Any help appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用所描述的连接字符串格式在这里。如果您在域上,并且将使用客户端上的登录帐户访问SQL Server,则可以使用受信任的连接,
否则您需要提供凭据。
Try using the connection string format described here. If you are on a domain and will be using the logged-in account on the client to access the SQL server you can use a trusted connection
Otherwise you'll need to provide credentials.