title: Connecting to a SQL Server in another domain
body: <p>I have a SQL Server in another domain that I need to query against.  I haven't been able to find anything online similar to what I'm trying to do so I'm coming here for help.  Normally, I've been using Window's Remote Desktop Connection to log onto the other domain and then I would open management studio from the remote machine and execute my queries.</p>
  
  <p>I would like to have code do what remote desktop connection does without the graphical interface.  I just want my code to remote into the other domain, execute a query, and disconnect.  Here's what I have:</p>
  
  <pre><code>Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
      Dim connectionString As String = "Server = 111.111.111.111; Initial Catalog = remote database; Network Library=DBMSSOCN; integrated security = true; User ID = domain2\mylogin; Password = myPass;"
      Dim connectionString2 As String = "Server = local SQL Server; Initial Catalog = local database; integrated security=true"
  
      Try
          Using sourceConnection As SqlConnection = _
          New SqlConnection(connectionString)
  
              Dim commandSourceData As SqlCommand = New SqlCommand( _
                                  "SQL Query", sourceConnection)
  
              sourceConnection.Open()
              Dim reader As SqlDataReader = commandSourceData.ExecuteReader()
  
              Using destinationConnection As SqlConnection = _
                  New SqlConnection(connectionString2)
                  destinationConnection.Open()
                  Using bulkCopy As SqlBulkCopy = _
                      New SqlBulkCopy(destinationConnection)
                      bulkCopy.ColumnMappings.Add("Column1", "Column1")
                      bulkCopy.DestinationTableName = "local database"
                      bulkCopy.WriteToServer(reader)
                  End Using
                  destinationConnection.Close()
              End Using
              reader.Close()
              sourceConnection.Close()
          End Using
      Catch ex As Exception
          MessageBox.Show(ex.Message)
      End Try
  End Sub
  </code></pre>
  
  <p>I get the following error:</p>
  
  <p><img src="https://i.stack.imgur.com/T3S6g.png" alt="enter image description here"></p>
  
  <p>Yes, my SQL Server is configured to allow remote connections.</p>