Import CSV to sql from asp.net c#

Here I will show how to Import data from csv file to sql database or any other database.

//This is the connection string to connect to your csv file
string strConString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\; Extended Properties=\”Text;HDR=YES;\”";

// open connection
OleDbConnection oCon = new OleDbConnection(strConString);
oCon.Open();

// fill data set
string strSql = “SELECT * FROM csvfile.csv”; //csvfile should be present in c:
OleDbDataAdapter oDA = new OleDbDataAdapter(strSql, oCon);
DataSet oData = new DataSet();
oDA.Fill(oData, “ABC”);
GridView1.DataSource = oData;
GridView1.DataBind();

oCon.Close();

6 Responses

  1. Any idea on how to specify a character set. I have an implmentation of this, but it has come to light that certain characters are not being read into the dataset set correctly.

    For example äüö is changed to äüö .

    Does anyone have any ideas, it there an additional parameter for the connection string or a property for the oleadapter for example that I am missing?

    Cheers.

  2. Hi rohitsies ,

    you really helped me out here. I spend quite some time to get a csv file into a dataset, but didn’t know the exact configuration.

    tnx a lot for your post!!

    Frank
    (the Netherlands)

  3. hello rohitsies,

    Thanks for such a nice dataset example,
    It really halped me.

    Thanks

  4. Hi rohitsies,

    Thank you very much for your example. It helped me a lot.

    Regards,
    Thant Zin

Leave a Reply