Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to upload a csv file to sql server 2005 via vb.net
upload from different csv file
Posted

1 solution

Hi,
First convert your csv file to DataTable, For your reference find the below codes.

VB
Private Function CSVtoDataTable(ByVal filepath As String) As DataTable
       Dim sr As New StreamReader(filepath)
       Dim f As File
       Dim fval As String()
       Dim dt As New DataTable
       Dim dr As DataRow

       sr = f.OpenText(filepath)

       fval = sr.ReadLine().Split(",")

       For i As Integer = 0 To fval.Length - 1
           dt.Columns.Add(New DataColumn(fval(i).ToString()))
       Next

       dr = dt.NewRow

       While sr.Peek() <> -1
           fval = sr.ReadLine().Split(",")
           dr = dt.NewRow
           For i As Integer = 0 To fval.Length - 1
               dr.Item(i) = fval(i).ToString()
           Next

           dt.Rows.Add(dr)
       End While

       Return dt

End Function


Then insert the datatable's records to the sql table using SqlBulkCopy method. Refer my tips from the following url

SQL Bulk copy method to insert large amount of data to the sql database[^]

Hope this will help you,

Regards,
Bluesathish
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900