.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 >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Datatable to sqlserver database in vb.net

Posted By: Riasul Hassan     Posted Date: May 07, 2011    Points:5   Category :VB.Net
Hi
I want to update bulk data available in datatable to SQL database without making loop. is it possible?


please send the coding in vb.net

regards
Riasul




Responses
Author: Maverick             
Posted Date: May 09, 2011     Points: 20   

Hello,

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
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

Hope this helps



Thanks,
Maverick


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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