Remote SQL Server connection string

心不动则不痛 提交于 2020-01-05 08:26:13

问题


I have had a project where SQL Server and development all worked locally.

The following connection string was used:

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

My development team is now growing and I am looking to have another developer access this same database.

He will need to alter this connection string and we are trying to workout what to change in the string - so far we have tried:

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

But the following error is thrown when the page is visited:

[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

The SQL Server has had the TCPIP protocol enabled and is listening on all IPAddress on my system. Port 1433 is added to the firewall is inbound for UDP and TCP and the SQL Server Express instance service has been restarted.

Can anybody please help us with the connection string? Additionally I feel the Integrated Security line will need to change.

To keep things simple I have moved the question with the updated status to a new thread : Connection string for SQL Server Express on remote Computer login failed error


回答1:


Not 100% sure if it causes that error, but I've seen this trip people up when remotely connecting to a SQL Server for the first time, so worth a check:

Open up the server properties in SSMS, under Connections, check to see if Allow remote connections to this server is checked. You'll need that checked to connect from another machine, even if on the same LAN.




回答2:


Check to see if the "Integrated Security = 'true'" is your problem.

If it is, this will mean that your remote connection will have to supply access/security credentials.




回答3:


MYDBTry a connection string like this:

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

(sub out the server and database parameters as needed)

Also, you mentioned that you opened ports which is good. There is another part to SQL configuration that you should look at. Go into the SQL Configuration Manager on your SQL server. Expand SQL Server Network Configuration. Open Protocols for {yourinstance}, and double click on TCP/IP. Under the IP Addresses tab, scroll to the very bottom. Make sure TCP Dynamic Ports is blank. Also verify that TCP Port is 1433 as you expect.

EDIT1: Updated the connection as per the comment suggestion.

Also - since you are remotely connecting in, take a real close look at the Trusted_Connection bit. Is your windows account that you running on your local machine able to log in and connect to SQL on the remote server? If you aren't doing anything remotely and this in the same subnet, you probably don't need to worry about this.

EDIT2:

Andy - based on your comment your issue appears to be that your connection is not trusted and you will need to enter a username and password. This is defined within SQL if you are using SQL logins. If you are using windows authentication, you need to add your domain user account to the allowed logins.

If you are using SQL logins, you will need to use a "standard security" style connection string, whereas if you are using a domain account you will need to use a "trusted connection" style connection string. See here for an example on the string:

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

Standard Security Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;

Trusted Connection Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

Now, you might be asking where to setup this SQL login! You will need to create both a SQL login and a database user. This can be done in 1 step from SQL Management Studio. Here is a detailed how-to from the MSDN:

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




回答4:


Finally worked it out.

Turning the security to be both Windows and SQL authentication. Then following : How can I create a user in SQL Server Express database I added to my project? followed by GRANT EXECUTE to all stored procedures fixed us up with exactly what we needed!



来源:https://stackoverflow.com/questions/24540126/remote-sql-server-connection-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!