In this article I will explain with an example, how to import / upload CSV file data to SQL Server database in ASP.Net using C# and VB.Net.
The CSV file data will be inserted into SQL Server database table using the SqlBulkCopy class.
I have made use of the following table Customers with the schema as follows.
Import (Upload) CSV file data to SQL Server database in ASP.Net using C# and VB.Net
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
HTML Markup
The HTML Markup consists of ASP.Net FileUpload control and a Button to trigger the file upload process.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button Text="Upload" OnClick="Upload" runat="server" />
You will need to import the following namespaces.
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Import / Upload CSV file data to SQL Server database table
When the Upload Button is clicked, the CSV file is uploaded and saved in a Folder named Files.
The CSV file data is read into a String variable using the File class ReadAllText method.
A DataTable is created with columns same as that of the destination database table and then the CSV file data is split using New Line (\n) and Comma (,) characters and using a loop the data is saved into the DataTable.
Once the connection is established with the Database and the SqlBulkCopy object is initialized, the name of the Table is specified using the DestinationTableName property.
Finally the DataTable is used by SqlBulkCopy to insert data into the SQL Server database table.
protected void Upload(object sender, EventArgs e)
    //Upload and save the file.
    string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
            new DataColumn("Name", typeof(string)),
            new DataColumn("Country",typeof(string)) });
    string csvData = File.ReadAllText(csvPath);
    foreach (string row in csvData.Split('\n'))
        if (!string.IsNullOrEmpty(row))
            int i = 0;
            foreach (string cell in row.Split(','))
                dt.Rows[dt.Rows.Count - 1][i] = cell;
    string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(consString))
        using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
            //Set the database table name.
            sqlBulkCopy.DestinationTableName = "dbo.Customers";
Protected Sub Upload(sender As Object, e As EventArgs)
    'Upload and save the file.
    Dim csvPath As String = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName)
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
    Dim csvData As String = File.ReadAllText(csvPath)
    For Each row As String In csvData.Split(ControlChars.Lf)
        If Not String.IsNullOrEmpty(row) Then
            Dim i As Integer = 0
            For Each cell As String In row.Split(","c)
                dt.Rows(dt.Rows.Count - 1)(i) = cell
                i += 1
        End If
    Dim consString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(consString)
        Using sqlBulkCopy As New SqlBulkCopy(con)
            'Set the database table name.
            sqlBulkCopy.DestinationTableName = "dbo.Customers"
        End Using
    End Using
End Sub
CSV File
Import (Upload) CSV file data to SQL Server database in ASP.Net using C# and VB.Net
Table containing the data from the CSV file
Import (Upload) CSV file data to SQL Server database in ASP.Net using C# and VB.Net