//Upload and save the file string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(fuLeadupload.PostedFile.FileName); fuLeadupload.SaveAs(excelPath); string conString = string.Empty; string extension = Path.GetExtension(fuLeadupload.PostedFile.FileName); switch (extension) { case ".xls": //Excel 97-03 conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString; break; case ".xlsx": //Excel 07 or higher conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString; break; } conString = string.Format(conString, excelPath); using (OleDbConnection excel_con = new OleDbConnection(conString)) { excel_con.Open(); string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString(); DataTable dtExcelData = new DataTable(); //[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default. dtExcelData.Columns.AddRange(new DataColumn[2] { //new DataColumn("LeadID", typeof(int)), new DataColumn("CustomerFirstName", typeof(string)), new DataColumn("CustomerLastName",typeof(string)) }); using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con)) { oda.Fill(dtExcelData); } excel_con.Close(); string consString = ConfigurationManager.ConnectionStrings["Retention"].ConnectionString; using (SqlConnection con = new SqlConnection(consString)) { using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con)) { //Set the database table name sqlBulkCopy.DestinationTableName = "dbo.tbl_LeadMasterDetails"; //[OPTIONAL]: Map the Excel columns with that of the database table sqlBulkCopy.ColumnMappings.Add("Name", "Name"); sqlBulkCopy.ColumnMappings.Add("Addresse", "Address"); con.Open(); sqlBulkCopy.WriteToServer(dtExcelData); con.Close(); } } }
string CreatedBY=Session["Username"].toString(); Datetime CreatedOn=DateTime.Now
"SELECT FirstName,Lastname,Address,"+CreatedBY+","+CreatedOn+"FROM [Sheet1$]"
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)