I don't know why data isn't getting into SQL. Can someone please review this and see what's wrong with my code? I got most of the code below from and MSDN page: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy(v=vs.110).aspx
Dim connectionString As String = "Server= "<servername>"; integrated security=true"
Using sourceConnection As SqlConnection = _
New SqlConnection(connectionString)
sourceConnection.Open()
Dim commandSourceData As SqlCommand = New SqlCommand(<TSQL>), sourceConnection)
Dim reader As SqlDataReader = commandSourceData.ExecuteReader
Using destinationConnection As SqlConnection = _
New SqlConnection(connectionString)
destinationConnection.Open()
Using bulkcopy As SqlBulkCopy = _
New SqlBulkCopy(destinationConnection)
bulkcopy.DestinationTableName = _
"<tableName>"
Try
bulkcopy.WriteToServer(reader)
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
reader.Close()
End Try
End Using
End Using
sourceConnection.Close()
End Using
bulkcopy.BatchSize = 500andbulkcopy.BulkCopyTimeout=2400beforebulkcopy.WriteToServer(reader). The batchsize optimizes the run, but shouldn't matter much in this case, you can play around with values to see which number works best for u.bulkCopy.BulkCopyTimeoutmay actually help if you are experiencing timeouts.ColumnMappingswhile bulk-copying to a temp table. PS: just to rule our something silly and obvious,<tableName>is not a real destination table name, is it?SELECT TOP 0 * INTO NewTable FROM YourInputSelectTable. This will copy the field structure into the new table and copy the data. In theory, you won't need ColumnMappings for this task.