.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Bulk copy in vb.net

Posted By:Maverick       Posted Date: March 24, 2011    Points: 40    Category: SQL Query    URL: http://www.dotnetspark.com  

This SQL code is used to Bulk copy using vb.net
 

 Public Shared Function InsertQueries(ByVal DestinationTable As String, ByVal dt As DataTable, ByVal keepIdentity As Boolean, ByVal ds As DataSet) As Int64
        Dim bulkCopy As SqlBulkCopy
        Dim strConnection As String = ConfigurationManager.ConnectionStrings("TargetConnection").ConnectionString
        bulkCopy = New SqlBulkCopy(strConnection, SqlBulkCopyOptions.KeepNulls)
        Dim destcolumn As New ArrayList
        Dim srcColumn As New ArrayList
        Dim i As Integer

Dt contains the list columns for source table

        If ds.Tables(0).Rows.Count <> dt.Columns.Count Then
            ds.Tables(0).Rows.RemoveAt(0)
        End If
        Dim mappedColumns As ArrayList = New ArrayList()
        For i = 0 To dt.Columns.Count - 1
        Dim mapName As New           SqlBulkCopyColumnMapping(dt.Columns(i).ColumnName.ToString(), ds.Tables(0).Rows(i).Item(0).ToString())
            mappedColumns.Add(mapName)
        Next
        For i = 0 To mappedColumns.Count - 1
            bulkCopy.ColumnMappings.Add(mappedColumns(i))
        Next
        bulkCopy.DestinationTableName = DestinationTable
        bulkCopy.WriteToServer(dt)
        bulkCopy.Close()
    End Function

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend