Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to import data from Excel into SQL Server Express 2008 in MVC3
    primarykey
    data
    text
    <p>I'm using MVC3 with C# code. I have a table in my SQL Server Express containing some columns, and I have an Excel sheet which has the same number of columns with the same datatypes and names. </p> <p>My requirement is I want to browse that Excel file in my MVC3 application, where the user can select the file. There is a <code>RegistrationNo</code> column in both my database table as well as in the Excel sheet. Before importing the data in the table of the database the <code>RegNo</code> present in the Excel sheet should be compared with the <code>RegNo</code> in the database table and if that <code>RegNo</code> already exists than no insertion for that <code>RegNO</code> should take place else if that <code>RegNo</code> is not present in the table than the row for that <code>RegNo</code> should be inserted.</p> <p>Below is my code that I have tried but I'm getting lot of issues with that.</p> <pre><code>[HttpPost] public ActionResult AdmissionUpload() { string filePath = null; foreach (string inputTagName in Request.Files) { HttpPostedFileBase Infile = Request.Files[inputTagName]; if (Infile.ContentLength &gt; 0 &amp;&amp; (Path.GetExtension(Infile.FileName) == ".xls" || Path.GetExtension(Infile.FileName) == ".xlsx" || Path.GetExtension(Infile.FileName) == ".xlsm")) { filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(Infile.FileName)); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } Infile.SaveAs(filePath); //Infile.SaveAs(filePath); } if (filePath != null) { System.Data.OleDb.OleDbConnection oconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath.ToString() + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";"); oconn.Open(); try { if (oconn.State == System.Data.ConnectionState.Closed) oconn.Open(); } catch (Exception ex) { // MessageBox.Show(ex.Message); } dynamic myTableName = oconn.GetSchema("Tables").Rows[0]["TABLE_NAME"]; OleDbCommand ocmd = new OleDbCommand("select * from [" + myTableName + "]", oconn); OleDbDataReader odr = ocmd.ExecuteReader(); if (odr.HasRows) { while (odr.Read()) { if (odr[0].ToString().Trim() != "") { if (CheckDepartment(odr[0].ToString().Trim()) == false) { var model = new DepartmentMaster(); model.DepartmentName = odr[1].ToString().Trim(); db.DepartmentMasters.AddObject(model); db.SaveChanges(); FLAG = true; } } } } } } return View(); } </code></pre> <p>Here <code>CheckRegNo</code> checks whether the <code>RegNo</code> exists.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload