Importing Data from Excel sheet to the Database
-
Hi... I am developing a project called Info Dial....I got stuck at a place where i have to store an excel sheet into a database that can be displayed in a grid view....thats working fine for a single Excel sheet...but what if i have more than one excel shhets :( ??How can i overcome this problem? I have my code as shown.... if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0) { string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName); string savelocation = @"D:\programs\" + fn; FileUpload1.PostedFile.SaveAs(savelocation); string excelConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + savelocation + ";" + "Extended Properties=Excel 8.0;"); // Create Connection to Excel Workbook OleDbConnection connection = new OleDbConnection(excelConnectionString); OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection); connection.Open(); // Create DbDataReader to Data Worksheet System.Data.OleDb.OleDbDataReader dr = command.ExecuteReader(); // SQL Server Connection String string sqlConnectionString = "Database=Infodial;server=s156;uid=sa;pwd=sa"; // Bulk Copy to SQL Server SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString); bulkCopy.DestinationTableName = "companydetails"; bulkCopy.WriteToServer(dr); connection.Close(); ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", "<script>alert('Saved in Database Successfully');</script>", false); } Any code would be appreciated... Thanks in Advance Regards Abhishek.