title: C# - Connect to remote SQL Server securely?
body: <p>I know it's easy to connect to a SQL Server database however I'm not sure how I'm supposed to do it remotely and on the same time.. with a secure way .</p>
  
  <pre><code>SqlConnection sqlConnection = this.sqlcon();
  
  SqlCommand insertCommand = new SqlCommand("use " + database_telecaster.ToString() + " SELECT Top 1  sid from dbo.Item order by sid desc", sqlConnection);
  
  sqlConnection.Open();
  insertCommand.ExecuteNonQuery();
  
  SqlDataReader reader = insertCommand.ExecuteReader();
  
  while (reader.Read())
  {
      MaxSid = (reader.GetInt64(0) + 100).ToString();
  }
  reader.Close();
  sqlConnection.Close();
  </code></pre>
  
  <p>SQL Server con function :</p>
  
  <pre><code>public SqlConnection sqlcon()
  {
      var doc = new XPathDocument(Application.StartupPath + "/DBConn.xml");
      var navigator = doc.CreateNavigator();
  
      var serverName = navigator.SelectSingleNode("//appsettings/servername");
  
      var username = navigator.SelectSingleNode("//appsettings/username");
      var password = navigator.SelectSingleNode("//appsettings/password");
      var database = navigator.SelectSingleNode("//appsettings/database");
  
      object[] objArray = new object[] {
              serverName , database, username , password 
      };
  
      return new SqlConnection(string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};MultipleActiveResultSets = True", objArray));
  }
  </code></pre>
  
  <p>Assuming that the SQL Server is on a  Windows VPS installed and I'm going to give my software to different people and I want them all to access that SQL server... how I can do that without opening the ports of the SQL Server ? Because as far as I know opening the ports will lead to getting hacked since all people will be able to connect remotely to that server .</p>