【问题标题】:Remote SQL Server connection string远程 SQL Server 连接字符串
【发布时间】:2014-07-02 20:07:12
【问题描述】:

我有一个项目,其中 SQL Server 和开发都在本地工作。

使用了以下连接字符串:

  SqlConnection connection= new SqlConnection("Data Source=.\\MYDB;Initial Catalog=Database;Integrated Security=true");
  connection.Open();

我的开发团队现在正在壮大,我希望让其他开发人员访问同一个数据库。

他将需要更改此连接字符串,我们正在尝试尝试更改字符串中的哪些内容 - 到目前为止我们已经尝试过:

  SqlConnection connection= new SqlConnection("Data Source=2.221.1.145\\MYDB;Initial Catalog=Database;Integrated Security=true");
  connection.Open();

但是在访问页面时会抛出如下错误:

[SqlException (0x80131904): 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)]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5352431
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5363103
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +922
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) +518
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +278
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +38

SQL Server 已启用 TCPIP 协议并正在侦听我系统上的所有 IPAddress。端口 1433 已添加到防火墙,是 UDP 和 TCP 的入站端口,并且 SQL Server Express 实例服务已重新启动。

任何人都可以帮助我们连接字符串吗?此外,我认为集成安全线需要更改。

为了简单起见,我已将更新状态的问题移至新线程:Connection string for SQL Server Express on remote Computer login failed error

【问题讨论】:

  • 如果我在连接时遇到问题,我通常会像您一样使用 IP 地址。但是...您尝试过服务器的 DNS 名称吗?
  • 是的 - 我也遇到了同样的问题!
  • 您运行的是什么版本的 SQL?它是“localDB”版本吗? SELECT @@VERSION 会很容易告诉你。
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 您是否要让他们连接到您的本地计算机 SQL 实例?

标签: c# asp.net sql-server connection-string sql-server-2012-express


【解决方案1】:

不是 100% 确定它是否会导致该错误,但我在第一次远程连接到 SQL Server 时看到了这种情况,因此值得一试:

在 SSMS 中打开服务器属性,在 Connections 下,检查是否选中了 Allow remote connections to this server。即使在同一个 LAN 上,您也需要选中该选项才能从另一台计算机连接。

【讨论】:

  • 已检查 :) - 但不能解决问题。
【解决方案2】:

检查“Integrated Security = 'true'”是否是您的问题。

如果是,这意味着您的远程连接必须提供访问/安全凭据。

【讨论】:

    【解决方案3】:

    MYDB试试这样的连接字符串:

    SqlConnection connection= new SqlConnection("Server=2.221.1.145;Database=Database;Trusted_Connection=True");
    

    (根据需要分出服务器和数据库参数)

    另外,你提到你打开了端口,这很好。您应该查看 SQL 配置的另一部分。进入 SQL 服务器上的 SQL 配置管理器。展开 SQL Server 网络配置。打开 {yourinstance} 的协议,然后双击 TCP/IP。在 IP 地址选项卡下,滚动到最底部。确保 TCP 动态端口为空白。还要验证 TCP 端口是否为您所期望的 1433。

    编辑1: 根据评论建议更新了连接。

    另外 - 由于您是远程连接,因此请仔细查看 Trusted_Connection 位。您在本地计算机上运行的 Windows 帐户是否能够登录并连接到远程服务器上的 SQL?如果你没有在远程做任何事情并且这在同一个子网中,你可能不需要担心这个。

    EDIT2:

    Andy - 根据您的评论,您的问题似乎是您的连接不受信任,您需要输入用户名和密码。如果您使用 SQL 登录,则在 SQL 中定义。如果您使用的是 Windows 身份验证,则需要将您的域用户帐户添加到允许的登录名中。

    如果您使用 SQL 登录,则需要使用“标准安全”样式的连接字符串,而如果您使用域帐户,则需要使用“可信连接”样式的连接字符串。有关字符串的示例,请参见此处:

    https://www.connectionstrings.com/sql-server-2012/

    标准安全 服务器=myServerAddress;数据库=myDataBase;用户 ID=myUsername; 密码=我的密码;

    可信连接 服务器=myServerAddress;Database=myDataBase;Trusted_Connection=True;

    现在,您可能会问在哪里设置此 SQL 登录!您将需要创建 SQL 登录名和数据库用户。这可以通过 SQL Management Studio 一步完成。这是来自 MSDN 的详细操作方法:

    http://msdn.microsoft.com/en-us/library/aa337562.aspx

    【讨论】:

    • 我认为 OP 正在尝试连接到一个命名实例——在本例中为“MYDB”。
    • 根据 OP 提供的内容,“MYDB”是 SQL Server 实例的名称,而不是数据库的名称。如果他的 IP 地址正确,那么他的连接字符串可能没有问题。
    • 我们说的是同一件事吗?我只是指出连接字符串的一个参数是Data Source=SqlServerName 用于SQL Server 的默认实例-或-Data Source=SqlServerName\InstanceName 用于SQL Server 的命名实例。在我的环境中,如果您尝试连接到命名实例,但仅提供 SqlServer DNS 名称(或 IP 地址),您会收到 OP 遇到的相同错误(“建立时发生与网络相关或特定于实例的错误一个连接...")
    • 使用您的字符串 - 我们得到一个登录错误....但我似乎无法获得正确的 UserId 和 Password 变量....它们应该是什么?如何将该用户添加到 SQL Server?
    • 它只是说:用户'Andy'登录失败
    【解决方案4】:

    终于解决了。

    将安全性设置为 Windows 和 SQL 身份验证。然后如下:How can I create a user in SQL Server Express database I added to my project? 后跟 GRANT EXECUTE to all stored procedures 为我们解决了我们需要的问题!

    【讨论】:

      猜你喜欢
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多