简体   繁体   中英

How to import Excel file to Database ADO.net

I tried to write code to import an Excel file to a database using C# and ADO.net I finally got the code to run but the output is wrong.

using Excel;
using System.IO;

namespace ExpPerson
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog op = new OpenFileDialog();
                op.Filter = "Excel Workbook| *.xls;*.xlsx;*.xlsm";
                if (op.ShowDialog() == DialogResult.Cancel)
                    return;
                FileStream stream = new FileStream(op.FileName, FileMode.Open);
                IExcelDataReader excelreader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                DataSet result = excelreader.AsDataSet();

                MaamoonKhalidIssueEntities db = new MaamoonKhalidIssueEntities();
                    foreach (DataTable table in result.Tables)
                    {
                        foreach (DataRow dr in table.Rows)
                        {
                            Person addtable = new Person()
                            {
                                nname = Convert.ToString(dr[0]),
                                ncode = Convert.ToString(dr[1]),
                                nTel1 = Convert.ToString(dr[2]),
                                nTel2 = Convert.ToString(dr[3]),
                                nFax = Convert.ToString(dr[4]),
                                nEmail = Convert.ToString(dr[5]),
                                nAdd = Convert.ToString(dr[6])
                            };
                        }



                }
                db.SaveChanges();
                excelreader.Close();
                stream.Close();

                MessageBox.Show("Import Sucess","Good",MessageBoxButtons.OK,MessageBoxIcon.Hand);
            }


            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

But there is an issue where the code runs without any errors but the data in the database isn't right and I can't figure out what is wrong.

You need to call db.People.Add(addtable); on each iteration of the DataRow loop.

So:

foreach (DataRow dr in table.Rows)
{
    Person addtable = new Person()
    {
        nname = Convert.ToString(dr[0]),
        ncode = Convert.ToString(dr[1]),
        nTel1 = Convert.ToString(dr[2]),
        nTel2 = Convert.ToString(dr[3]),
        nFax = Convert.ToString(dr[4]),
        nEmail = Convert.ToString(dr[5]),
        nAdd = Convert.ToString(dr[6])
    };
    db.People.Add(addtable)
}

Otherwise, Entity Framework doesn't know you want them inserted to the database.

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