Upload a Excel file in Oracle Database Using asp.net

ASPX Page
  1. <table>  
  2.  <tr>  
  3. <td>  
  4. <asp:FileUpload ID="fUpload" runat="server" />  
  5. </td>  
  6. </tr>  
  7. <tr>  
  8. <td>  
  9. <asp:Button ID="btnUpload" runat="server" Text="Upload Excel"  
  10. onclick="btnUpload_Click" />  
  11. </td>  
  12. </tr>  
  13. </table>  
.CS File 
  1. protected void btnUpload_Click(object sender, EventArgs e)  
  2. {  
  3.  try  
  4.  {  
  5.     if (fUpload.HasFile)  
  6.      {  
  7.         string Path = Server.MapPath(fUpload.FileName);  
  8.         FillDatasetExcel(Path);  
  9.      }  
  10.  }  
  11.  catch (Exception ex)  
  12.  {  
  13.    throw ex;  
  14.  }  
  15. }  
  16. public DataTable FillDatasetExcel(string filepath)  
  17. {  
  18.   string path = filepath;  
  19.   // Prepare cnnection string  
  20.   string connectionstring =  
  21.  @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0 Xml;HDR=YES;FMT=Delimited';";  
  22.  byte[] Bytes = fUpload.FileBytes;  
  23.   File.WriteAllBytes(path, Bytes);  
  24.   OleDbConnection connection = new OleDbConnection();  
  25.   connection.ConnectionString = connectionstring;  
  26.   OleDbCommand command = new OleDbCommand();  
  27.   command.CommandType = CommandType.Text;  
  28.   command.Connection = connection;  
  29.   command.CommandText = "Select * FROM [Sheet1$]";  
  30.   connection.Open();  
  31.   // create data table object  
  32.   DataTable dt = new DataTable();  
  33.   // Execute Reader and load the data into datatable  
  34.   dt.Load(command.ExecuteReader());  
  35.   // Close the connection  
  36.   connection.Close();  
  37.   return dt;  
  38. }  
  39. }