1

我有一个SQL Server和开发都在本地工作的项目。远程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

+0

如果我在连接时遇到问题,我经常使用IP地址。但是...你是否尝试过DNS服务器的名称? – DMason

+0

是的 - 我得到同样的问题! – RenegadeAndy

+0

你正在运行什么版本的SQL?它是“localDB”版本吗? 'SELECT @@ VERSION'会很容易地告诉你。 – DMason

回答

0

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; Password = myPassword;

可信连接 Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;

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

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

+0

我认为OP正试图连接到一个命名实例 - 在本例中是“MYDB”。 – DMason

+0

很好,谢谢! –

+0

根据OP提供的内容,“MYDB”是SQL Server实例的名称,而不是数据库的名称。如果他有正确的IP地址,那么他的连接字符串可能没有问题。 – DMason

1

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

打开SSMS中的服务器属性,在连接下,检查是否检查Allow remote connections to this server。即使在同一个局域网上,您也需要检查它以连接另一台计算机。

+0

这是检查:) - 但不能解决问题。 – RenegadeAndy

0

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

如果是这样,这意味着您的远程连接将不得不提供访问/安全证书。