简体   繁体   中英

Uploading an Excel sheet and importing the data into SQL Server Express database

I am using an example from a post I found. This seems to be a good fit for what I am doing. Here is the code to save the file:

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase excelFile)
{
   //Save the uploaded file to the disc.
   string savedFileName = "~/App_Data/uploads/";// +excelFile.FileName;
   string filePath = Path.Combine(savedFileName, excelFile.FileName); 
   excelFile.SaveAs(Server.MapPath(filePath));

   return View();
}

Here is the code to put data into the database table:

private void SaveFileToDatabase(string savedFileName)
{
    String strConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename='catalog=QQAEntities'Integrated Security=SSPI;User Instance=True";
    String connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;", Server.MapPath(savedFileName));
    {
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
       using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM [dbo_ts_quality_audit_tbl$]", connection))
       {
          connection.Open();

          using (OleDbDataReader dReader = cmd.ExecuteReader())
          {
             using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
             {
                 //Give your Destination table name 
                 sqlBulk.DestinationTableName = "AuditSchedules";
                 sqlBulk.WriteToServer(dReader);
             }
          }
       }
       }
   }
}

private string GetLocalFilePath(string saveDirectory, FileUpload fileUploadControl)
{
   //System.Web.UI.WebControls.WebControl
   string filePath = Path.Combine(saveDirectory, fileUploadControl.FileName);

   fileUploadControl.SaveAs(filePath);

   return filePath;
}

Here is the view for FileUpload:

<h2>FileUpload</h2>

@using (Html.BeginForm("FileUpload", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" id="excelFile" name="excelFile" />
<input type="submit" value="Upload" />
}

The upload works. My question is; how do I invoke the data transfer ( SaveFileToDatabase ) in the view? This will all be done from admin section.

Also I already have a connection to the database in the config - how do I clean this up?

thanks

The view should obtain the file info, but then pass all execution into the controller. Inside of your controller, you should call your SaveFileToDatabase function and pass it the temp file that you saved, like so:

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase excelFile)
{
    //Save the uploaded file to the disc.
    string savedFileName = "~/App_Data/uploads/";// +excelFile.FileName;
    string filePath = Path.Combine(savedFileName, excelFile.FileName); 
    excelFile.SaveAs(Server.MapPath(filePath));

    // Call function to place temporary file into database
    SaveFileToDatabase(filePath);

    // Optional: Delete temporary Excel file from server

    return View();
}

(Don't forget to consider whether you want to persist the temporary Excel file on your server .. if not, you need to take care of deleting the file once you know processing has completed)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM