如何通过vb.net将CSV文件上传到SQL Server 2005 [英] how to upload a csv file to sql server 2005 via vb.net

查看:57
本文介绍了如何通过vb.net将CSV文件上传到SQL Server 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过vb.net将CSV文件上传到SQL Server 2005
从不同的CSV文件上传

how to upload a csv file to sql server 2005 via vb.net
upload from different csv file

推荐答案


首先将您的csv文件转换为DataTable,请查找以下代码,以供参考.

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

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



然后使用SqlBulkCopy方法将数据表的记录插入到sql表中.请参考以下网址的我的提示

SQL批量复制方法,可插入大量数据到sql数据库 [ ^ ]

希望这对您有帮助,

问候,
忧郁的



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


这篇关于如何通过vb.net将CSV文件上传到SQL Server 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆