Link to home
Start Free TrialLog in
Avatar of tenchulyo
tenchulyo

asked on

SQL Select from one sql server into another without linked servers in vb.net

Hi,

I need help finishing this import from one sql server to another with no linked servers. I got this far but I can't get passed the errors when importing:

  Dim SQLConnection As New SqlConnection()
        Dim ConnString2 As String = "Data Source=data;Initial Catalog=cat; User Id=sa; Password=blabla;"
        SQLConnection.ConnectionString = ConnString2

        Try
            SqlConnection.Open()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


        Dim expr As String = "SELECT City, State, Zip, home_phone as Phone FROM dbo.people"

        Dim objCmdSelect As SqlCommand = New SqlCommand(expr, SQLConnection)

        
        Dim objDR As SqlDataReader

        Dim SQLconn As New SqlConnection()
        Dim ConnString As String = ""Data Source=data2;Initial Catalog=cat2; User Id=sa; Password=blabla;"
        SQLconn.ConnectionString = ConnString
        SQLconn.Open()




        Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
            bulkCopy.DestinationTableN<wbr ></wbr><wbr ></wbr>ame = "Import"

            Try
                objDR = objCmdSelect.ExecuteReader<wbr ></wbr><wbr ></wbr>
                bulkCopy.WriteToServer(obj<wbr ></wbr><wbr ></wbr>DR)
                objDR.Close()
                SQLconn.Close()



                MsgBox("Import Completed Successfully!!")

Open in new window


Essentially I need to run an sql from one server

take those results and insert it into another server that is not linked (can't be linked)

I usually use an insert when it is a linked server but I can't do that in this case...

Does anyone know a good workaround?

Thanks
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Do you need to do that by code?
You can use SQL Server Management Studio Import/Export Data Wizard.
User generated image
SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tenchulyo
tenchulyo

ASKER

Hey Vitor,

Sorry but yes, It has to be done in code.

Hi Giannis,

Yes, that might work if I re-write everything and add one of those libraries, but your answer is more correct. After many hours yesterday I figured out that my code is correct, the information I was missing is that bulkcopy inserts the data from left to right and so your tabledata must reflect in that order. your query has to be left to right select in order for this to work. Now it works like a charm!

Thanks guys,

Lyo