How to import data from excel in c# to solve these error

Posted by Raghuldrag under C# on 11/21/2014 | Points: 10 | Views : 1085 | Status : [Member] | Replies : 1
Hi Friends,

i want to import data from excel into sql server table so i choose the c# and found a code from google

protected void btnSend_Click(object sender, EventArgs e)
string strconn = "Server = ERPSERVER3; Database = POMDB; User Id = sa; Password = sa;";
string path = fileuploadExcel.PostedFile.FileName;
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("SELECT [item_code],[item_desc],[uom],[grade],[Heads],[Moc],[variant],[Curr_Budget],[fin_yr] FROM [Sheet1$]", excelConnection);
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strconn);
//Give your Destination table name
sqlBulk.DestinationTableName = "pur_bud_pro_lc";

when i import the excel file its show Error of "Line 1: Incorrect syntax near 'COLLATE'." How to Solve that?


Posted by: Oliver32 on: 12/3/2014 [Member] Starter | Points: 25

You're using OleDb to import Excel Data to dataset, here is sample code that may help

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

Login to post response