getting error while uploading excel file into sql server [Resolved]

Posted by Rickeybglr under ASP.NET on 9/8/2011 | Points: 10 | Views : 1376 | Status : [Member] | Replies : 1
hi,

getting error "output table is not in correct format" can somebody tell me why ?? i am providing my code here..

where excel1 is name of excel file,
tb_excel is name of database table which have two column (id, name)
Sheet1 has data in two fileds id, name


try
{
SqlConnection sqlconnection = new SqlConnection("Data Source=.;Initial Catalog=demo_db;Integrated Security=true");
string strSource = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\e_diary_old_style\excel1.xlsx; Extended Properties=""Excel 8.0;HDR=YES;""";
OleDbConnection sSourceConnection = new OleDbConnection(strSource);
sqlconnection.Open();
using (sSourceConnection)
{
string sqlInsert = string.Format("select id,name from [{0}]", "Sheet1$");
OleDbCommand cmd = new OleDbCommand(sqlInsert, sSourceConnection);
sSourceConnection.Open();
using (OleDbDataReader dr = cmd.ExecuteReader())
{
SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlconnection);
bulkCopy.DestinationTableName = "tb_excel";
bulkCopy.WriteToServer(dr);

}
}

}




Responses

Posted by: Prabhakar on: 9/8/2011 [Member] [MVP] Starter | Points: 50

Up
0
Down

Resolved

Hi Rickeybglr

try this link...it's help u . .

http://www.codeproject.com/KB/database/Excel2Sql.aspx

Best Regard's
Prabhakar

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response