-->

远程SQL Server连接字符串(Remote SQL Server connection str

2019-10-20 09:47发布

我有一个项目,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协议,并侦听所有ip地址我的系统上。 端口1433添加到防火墙的入站的UDP和TCP和SQL Server Express实例服务已重新启动。

任何人都可以请帮助我们连接字符串? 此外,我觉得集成安全线将需要改变。

为了简单起见,我提出的问题与更新的状态,以一个新的线程: 连接字符串SQL Server Express的远程计算机登录失败错误

Answer 1:

不是100%肯定,如果它导致的错误,但我已经看到了这行的人了,当远程连接到SQL Server的第一次,所以值得一检查:

打开SSMS中的服务器属性,下连接,请检查是否Allow remote connections to this server被选中。 你需要的是检查从另一台机器,即使在同一个局域网上进行连接。



Answer 2:

请检查是否在“集成安全性=‘真’”是你的问题。

如果是这样,这将意味着你的远程连接将必须提供接入/安全证书。



Answer 3:

MYDBTry连接字符串是这样的:

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,你期望的那样。

EDIT1:更新了连接按注释建议。

另外 - 因为你是远程连接中,采取在Trusted_Connection位真正的密切关注。 是您的Windows帐户您在本地机器上运行能够登录并连接到SQL远程服务器上? 如果您没有在同一个子网中远程执行任何事情,这一点,你可能并不需要担心这个。

EDIT2:

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

如果您使用的是SQL登录,您将需要使用“标准安全”风格的连接字符串,而如果你使用的是域帐户,您将需要使用“可信连接”风格的连接字符串。 看到这里的字符串的例子:

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

标准安全服务器= myServerAddress;数据库= MyDatabase的;用户ID =名为myUsername; 密码= MYPASSWORD;

受信任的连接服务器= myServerAddress;数据库= MyDatabase的; Trusted_Connection = TRUE;

现在,你可能会问哪里设置这个SQL登录! 您需要同时创建SQL登录名和数据库用户。 这可以从SQL Management Studio中1步来完成。 这里是一个详细的如何,从MSDN:

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



Answer 4:

最后的工作了。

谈到安全性是Windows和SQL身份验证。 然后执行以下操作: 我怎样才能创建SQL Server Express数据库用户,我添加到我的项目? 其次授予执行到所有存储过程固定我们了,我们需要什么!



文章来源: Remote SQL Server connection string