Tuesday 19 June 2018

How to insert Bulk Data of Excel into Sql server Database







Form 1 Code:



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using Excel = Microsoft.Office.Interop.Excel;

using exceltosql.Model;



namespace exceltosql

{

    public partial class Form1 : Form

    {

        List<student> li = new List<student>();

        exceltosqlEntities db = new exceltosqlEntities();

        public Form1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            Excel.Application xlApp;



            Excel.Workbook xlWorkBook;



            Excel.Worksheet xlWorkSheet;



            Excel.Range range;







            int rw = 0;



            int cl = 1;







            xlApp = new Excel.Application();



            xlWorkBook = xlApp.Workbooks.Open(@"C:\images\docs\salman.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);



            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);







            range = xlWorkSheet.UsedRange;



            rw = range.Rows.Count;



           //



            //



            //data read code...............................................



            for (int i = 2; i <= rw; i++)

            {



               student s = new student();



                s.id = (int)(range.Cells[i, cl] as Excel.Range).Value;

                s.Enrolment = (string)(range.Cells[i, ++cl] as Excel.Range).Value;

                s.name = (string)(range.Cells[i, ++cl] as Excel.Range).Value;

                s.batchcode = (string)(range.Cells[i, ++cl] as Excel.Range).Value;

               

               cl = 1;



                li.Add(s);



            }







            //data read code...............................................







            //



            xlWorkBook.Close(true, null, null);



            xlApp.Quit();







            Marshal.ReleaseComObject(xlWorkSheet);



            Marshal.ReleaseComObject(xlWorkBook);



            Marshal.ReleaseComObject(xlApp);







            dataGridView1.DataSource = li;



        }



        private void button2_Click(object sender, EventArgs e)

        {

            foreach (var item in li)

            {

                tbl_student s = new tbl_student();

                s.s_id = item.id;

                s.s_name = item.name;

                s.s_enrollment = item.Enrolment;

                s.s_batchcode = item.batchcode;

                db.tbl_student.Add(s);

                db.SaveChanges();



            }

            MessageBox.Show("Data successfully inserted......");

        }

    }

}

---------------------------------------------------------------------------------------------------

student class Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exceltosql.Model
{
    class student
    {
        public int id { get; set; }

        public string Enrolment { get; set; }

        public string name { get; set; }

        public string batchcode { get; set; }

    }
}

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...