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:
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
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!!")
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
You can use SQL Server Management Studio Import/Export Data Wizard.