String connection to remote server

Raúl Sánchez 21 Reputation points
2021-01-13T10:37:20.953+00:00

Hello, everyone,

I have a question about how to connect a .NET application (programmed in C#) to a database (BaseX) of a SQL Server instance (ServerX) through a SQL user (Usser: UserX, Password: PasswordX) located in a remote server which can be accessed through a remote connection (IP: 193.160.160.240, user: UserZ, password: PasswordZ).

If I run the application from the server itself or a computer located in the network that has access to the server, the connection I make is as follows:
SqlConnection cn;
cn = new SqlConnection(Data Source=ServerX; Initial Catalog=BaseX; User ID=UserX; Password=PasswordX);

How should I specify in the connection string that remote connection to the server where the database is located?

Thank you.

SQL Server Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-01-15T03:12:03.663+00:00

    The connection string for connecting to a remote SQL server is generally written as:

    Data Source=ip,port;Network Library=DBMSSOCN;Initial Catalog=BaseX;User ID=UserX;Password=PasswordX;  
    

    If you haven't modified the port, you don't need to write it.

    This is not very different from the connection string you are using. Have you encountered any problems?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2021-01-13T10:55:51.013+00:00

    You address the server name, so as long as network server name resolving work, that connection string works for local as well as for remote connections.

    1 person found this answer helpful.
    0 comments No comments

  2. Abdulhakim M. Elrhumi 356 Reputation points
    2021-01-17T17:44:36.857+00:00

    Hi

    if (Settings.Default.ServerMode == "SQL")
       {
    string strcnn = "Data Source=192.168.100.215,1433; Network Library=DBMSSOCN;Initial Catalog=YourDB; User ID=YourUserName;Password=YourPassword";
    
    IDbConnection con = new SqlConnection(strcnn);
    
    con.Open();
    
      return con;
    }
    

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.