ASp.net c# sql

July 10, 2008

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();

Blog at WordPress.com.