hi
how can i export data from excel to sql server database.
can any body tell me the procedure how to do this and which is the better way to do this?
hi
how can i export data from excel to sql server database.
can any body tell me the procedure how to do this and which is the better way to do this?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
kesava reddyPosted Jul 15, 2008, 11:04 AM
hi thanks for giving me the sample code
but as i am completely new to this .net please send me the code in for loop
that how to insert into database;
SenPosted Jul 15, 2008, 6:26 AM
using System.Data.OleDb;
string sheetName = "Sheet1";
string filePath="C:\\temp.xls";
DataTable myDataTable = new System.Data.DataTable();
DataSet dataSet1 = new DataSet();
OleDbDataAdapter oledbAdapterObj;
OleDbConnection connection = new OleDbConnection();
OleDbDataAdapter command = new OleDbDataAdapter();
//connection to read excel file
connection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" + "data source='" + filePath + " '; " + "Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"");
connection.Open();
command = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetName + "$]", connection);
oledbAdapterObj = command;
oledbAdapterObj.Fill(dataSet1);
myDataTable = dataSet1.Tables[0];
for (int row = 0; row < myDataTable.Rows.Count; row++)
{
for (int col = 0; col < myDataTable.Columns.Count; col++)
{
//read data from datatable using
// (myDataTable.Rows[row][col].ToString());
}
//query to insert to sqlserver database
}