sql bulk copy from datatable into sql table(with transaction)
Dim con As New SqlConnection("connection string")
con.Open()
Dim myTransaction As SqlTransaction = con.BeginTransaction()
Dim bulkCopy As New SqlBulkCopy(con, SqlBulkCopyOptions.Default, myTransaction)
bulkCopy.DestinationTableName = "table name"
bulkCopy.WriteToServer("datatable")
myTransaction.Commit()
bulkCopy.Close()
Note:
you can use column mapping
bulkCopy.ColumnMappings.Add("column name in datatable", "column name in sql table")
Dim con As New SqlConnection("connection string")
con.Open()
Dim myTransaction As SqlTransaction = con.BeginTransaction()
Dim bulkCopy As New SqlBulkCopy(con, SqlBulkCopyOptions.Default, myTransaction)
bulkCopy.DestinationTableName = "table name"
bulkCopy.WriteToServer("datatable")
myTransaction.Commit()
bulkCopy.Close()
Note:
you can use column mapping
bulkCopy.ColumnMappings.Add("column name in datatable", "column name in sql table")