如何通过它将数据从一个数据库/表复制到另一个远程数据库/表

时间:2015-12-23 06:51:57

标签: sql-server vb.net

我需要通过它的IP将数据从一个数据库/表复制到另一个数据库/表 这是我的代码

INSERT INTO dtin select * From [66.22.100.70].MainData.dbo.dtin where dtin_code='ttr'

2 个答案:

答案 0 :(得分:1)

是的,但是您需要先做一些水管工作。

EXEC sp_addlinkedserver @server='192.168.0.200'

EXEC sp_addlinkedsrvlogin '192.168.0.200', 'false', NULL, 'sa', 'administrator' 

然后

insert into [192.168.0.200].webanalyzer.dbo.sale ([Url]
      ,[Title]
      ,[Subtitle]
      ,[Address]
      ,[Price]
      ,[LivingArea]
      ,[RoomCount]
      ,[PostCode]
      ,[TotalArea]
      ,[DateAdded]
      ,[DateRemoved]
      ,[DateLastProcessed]
      ,[ServiceCosts]
      ,[HeatingType]
      ,[IsBendrabutis]) 

select [Url]
      ,[Title]
      ,[Subtitle]
      ,[Address]
      ,[Price]
      ,[LivingArea]
      ,[RoomCount]
      ,[PostCode]
      ,[TotalArea]
      ,[DateAdded]
      ,[DateRemoved]
      ,[DateLastProcessed]
      ,[ServiceCosts]
      ,[HeatingType]
      ,[IsBendrabutis] from WebAnalyzer.dbo.Sale where url COLLATE DATABASE_DEFAULT not in
( 
select url COLLATE DATABASE_DEFAULT From [192.168.0.200].webanalyzer.dbo.sale
)

答案 1 :(得分:0)

使用SqlBulkCopy类:

可以轻松完成
Private Const SOURCE_CONNECTIONSTRING As String = "..."
Private Const TARGET_CONNECTIONSTRING As String = "..."


Using table As DataTable = New DataTable
    Using connection As SqlConnection = New SqlConnection(SOURCE_CONNECTIONSTRING)
        connection.Open
        Using command As SqlCommand = New SqlCommand("SELECT * FROM tableName", connection)
            Using adapter As SqlDataAdapter = New SqlDataAdapter(command)
                adapter.FillSchema(table, SchemaType.Source)
                adapter.Fill(table)
            End Using
        End Using
    End Using

    Using copier As SqlBulkCopy = New SqlBulkCopy(TARGET_CONNECTIONSTRING, SqlBulkCopyOptions.KeepIdentity Or SqlBulkCopyOptions.KeepNulls)
        For Each column As DataColumn In table.Columns
            If Not GetType(SqlGeography) Is column.DataType Then    
            '   Azure doesnt like geography      '
                copier.ColumnMappings.Add(column.ColumnName, column.ColumnName)
            End If
        Next

        copier.DestinationTableName = "tableName"
        copier.BulkCopyTimeout = 0

        copier.WriteToServer(table)
    End Using
End Using