Upload CSV file to SQL server

前端 未结 8 2085
悲哀的现实
悲哀的现实 2020-12-02 09:29

What is the best way to upload a large csv data file into SQL server using C# ? The file contains about 30,000 rows and 25 columns.

8条回答
  •  天涯浪人
    2020-12-02 10:06

    You can also use Bulk Insert

    Public Shared Function bulkQuery()
    
            Dim query As StringBuilder = New StringBuilder
    
            query.Append("USE Import_DB BULK INSERT dbo.[Insert_Table] FROM")
            query.Append(" 'C:\Insert_Table.csv' ")
            query.Append("With (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')")
    
            Return query.ToString
    
        End Function
    

    Be carfully here though since tablename and csv name has to be identical as well as the column count in the csv has to be the same as in the pre-defined table.

提交回复
热议问题