c# - 如何从另一台计算机访问SQL Server 2008中的数据库

标签 c# sql-server-2008

我需要使用 C# 访问另一台计算机上的数据库...我正在使用带有 IP 地址的连接字符串来访问数据库,还更改了防火墙设置,但它显示如下错误

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: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

我正在使用这个连接字符串

SqlConnection con = new SqlConnection("data source=192.168.1.12,1063;initial catalog=trinity;integrated security=false;network library=DBMSSOCN");

最佳答案

这实际上是一个很大的话题,涉及:网络、身份验证、授权和权限。快速 list :

  1. 在运行 SQL Server 的本地防火墙上打开端口 1433。
  2. 在运行 SQL Server 的本地机器上打开 SQL Server 配置管理器并启用 TCP 连接。
  3. 添加 Windows 登录或 SQL 帐户用户并授予对数据库的权限。永远不要使用 SA 帐户进行远程连接!

在您的连接字符串中有 Integrated security=false;,因此您需要在连接字符串中提供 User ID=mysqlserveruserid;Password=mysqlserverpassword; 参数。如果您更改 Integrated security=true;,将使用您的 Windows 凭据。

还有为什么是 IP 地址?您的网络上没有网络名称解析吗?例如DNS 或 WINS。

SQL Server 是作为默认实例运行还是具有命名实例?如果它是命名实例(安装 SQL Server 时会选择)您将需要 Data source=computername\sqlserverinstancename;

可以在 MSDN 上找到有关所有这些方面的更多详细信息 https://msdn.microsoft.com/en-us/library/ms345332.aspx

关于c# - 如何从另一台计算机访问SQL Server 2008中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28191301/

相关文章:

c# - 如何从 Windows 窗体 DateTimePicker 控件复制日期值?

c# - 协变地交换列表中的两个元素

sql - 使用 SQL 确保随机数不相同

sql - 如何将日期时间字段添加到 + 1 天

mysql - 我无法将数据从主表复制到临时表

c# - AlreadyAllocated 调用 waveOutOpen 错误

c# - 如何根据枚举的名称动态创建对象?无开关

c# - 如何在程序中进一步使用switch语句内分配的变量?

c# - 对分数和带分数列表进行排序

sql - 为数据表中的每一行选择 COUNT(foreign ID)