Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 3 columns in database table1
i want to store the excel sheet into table 1 the excel sheet has 3 colmns also
how to do that
Posted

1 solution

 
Share this answer
 
Comments
Member 11970398 10-Dec-15 2:28am    
here u have written

dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if it is sql server database then how to write this line
dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
Member 11970398 10-Dec-15 2:28am    
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
i use SqlConnection excelConnection = new SqlConnection (excelConnectionString);
Member 11970398 11-Dec-15 5:26am    
public ActionResult Upload(HttpPostedFileBase uploadFile)
{
String strConnection = "Data Source=Server=BANKING1\\MSSQLSERVER1;initial catalog=Online; User ID=sa;Password=Aldd439";


//file upload path
string path = uploadFile.ToString();
//Create connection string to Excel work book
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 [ID],[Name],[Designation] from [Sheet1$]",excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "ALQuestion.QuestionMaster";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900