Using SqlBulkCopy to implement an Excel bulk Import database

Source: Internet
Author: User
Tags import database

Introduction:

When importing Excel data, it is slow to insert using LINQ operations because of the large amount of data. Consider importing data using the ADO database method. Bulk import is achieved by using SqlBulkCopy for large data volumes.

A. Code Explanation:

1. Save the Excel upload you want to import to the server.

2. Use OleDbDataAdapter to get data from Excel into tab.

3. Create a table NEWDT in memory and update the tab table to NEWDT.

4. Use SqlBulkCopy to insert the NEWDT memory table data into the database table Sys_stuuser in bulk.

Two. The code is as follows:
 String FileUrl = String.                Empty; foreach (httppostedfilebase file in filetoupload) {string path = System.IO.Path.Combine (Server.MapPath ("~/models/stu/uploadexc"), System.IO.Path.GetFileName (DateTime.Now.ToString ("YYYYMMDDHHMMSS") + File.                    FileName)); File.                SaveAs (path);//saveas the uploaded file contents on the server FILEURL = path; string result = String.                Empty;                String strconn;                strconn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data source=" + FileUrl + ";" + "Extended properties=excel 8.0;";                OleDbConnection conn = new OleDbConnection (strconn);                OleDbDataAdapter mycommand = new OleDbDataAdapter ("SELECT * from [sheet1$]", conn);                DataSet myDataSet = new DataSet ();                Mycommand.fill (myDataSet, "excelinfo"); System.Data.DataTable tab = mydataset.tables["Excelinfo"].              Defaultview.totable ();  var majordata = majorinfoservice.loadentities (U = True).                ToList (); var schooldata = universityinfoservice.loadentities (U = True).                ToList (); // ...                Use foreach to add tab data to the database DataTable NEWDT = new DataTable ();                NEWDT.COLUMNS.ADD ("LoginID", typeof (String));                NEWDT.COLUMNS.ADD ("Pwd", typeof (String));                NEWDT.COLUMNS.ADD ("UserName", typeof (String));                NEWDT.COLUMNS.ADD ("Education", typeof (int));                NEWDT.COLUMNS.ADD ("Major", typeof (int));                NEWDT.COLUMNS.ADD ("Stuorigin", typeof (String));                NEWDT.COLUMNS.ADD ("Gender", typeof (int));                NEWDT.COLUMNS.ADD ("Destination", typeof (int));                NEWDT.COLUMNS.ADD ("Hometel", typeof (String));                NEWDT.COLUMNS.ADD ("Tel", typeof (String));                NEWDT.COLUMNS.ADD ("Emai", typeof (String));                NEWDT.COLUMNS.ADD ("StudentID", typeof (String)); Newdt.coluMns.                ADD ("School", typeof (int));                NEWDT.COLUMNS.ADD ("Gradyear", typeof (String));                NEWDT.COLUMNS.ADD ("Identityid", typeof (String));                NEWDT.COLUMNS.ADD ("Ifjoin", typeof (String));                NEWDT.COLUMNS.ADD ("Vcode", typeof (String));                NEWDT.COLUMNS.ADD ("Flag", typeof (int));                NEWDT.COLUMNS.ADD ("SID", typeof (String)); for (int i = 0; i < tab. rows.count-1; i++) {DataRow dr = tab.                    Rows[i]; String Schooltag = dr["School Code"]. ToString ().                    Trim (); String Majorid = dr["Professional Code"]. ToString ().                    Trim (); String DestinationId = dr["whereabouts of graduation"]. ToString ().                    Trim (); String Educationid = dr["Education Code"]. ToString ().                    Trim ();                        var SchoolID = (from S in schooldata where s.ucode = = Schooltag Select new {s.uid}).                    FirstOrDefault ();                       var Majorid = (from M in majordata where m.majorcode = = Majorid Select new {M.majorid}).                    FirstOrDefault ();                    DataRow NewRow = Newdt.newrow ();                    newrow["PWD"] = "0b946149d7dde1a273f26402a344008e"; newrow["UserName"] = dr["Name"]. ToString ().                    Trim ();                        Switch (Educationid) {case "newrow[": "Education"] = 4; Case "One": newrow["education"] = 3;                        Break Case "to": newrow["education"] = 2;                        Break Case "newrow[": "Education"] = 1;                        Break Default:newrow["Education"] = 0;                    Break } newrow["Major"] = Majorid = = null? 0:majorid.                    Majorid; newrow["Stuorigin"] = dr["origin"]. ToString ().                    Trim (); newrow["Gender"] = dr["Gender Code"] = = null? 0:convert.toint32 (dr["Gender Code"). ToString ().                    Trim ());       Switch (DestinationId)             {case "newrow[": "Destination"] = 1; Case "newrow[": "Destination"] = 2;                        Break Case "newrow[": "Destination"] = 3;                        Break Case "newrow[": "Destination"] = 4;                        Break default:newrow["Destination"] = 0;                    Break } newrow["Hometel"] = dr["Home Phone"]. ToString ().                    Trim (); newrow["Tel"] = dr["mobile number"]. ToString ().                    Trim (); newrow["Emai"] = dr["e-mail"]. ToString ().                    Trim (); newrow["StudentID"] = dr["School Number"]. ToString ().                    Trim (); newrow["School"] = SchoolID = = null? 0:schoolid.                    UID; newrow["gradyear"] = dr["Graduation Year"]. ToString ().                    Trim (); newrow["Identityid"] = dr["Social Security Number"]. ToString ().                    Trim ();                NEWDT.ROWS.ADD (NewRow); } SqlBulkCopy SqlBulkCopy = new SqlBulkCopy (PubConstant.connectionstring, sqlbulkcopyoptions.useinternaltransaction); SqlBulkCopy. DestinationTableName = "Sys_stuuser";//the table name in the database is SqlBulkCopy.                Columnmappings.add ("pwd", "pwd"); SqlBulkCopy.                Columnmappings.add ("UserName", "UserName"); SqlBulkCopy.                Columnmappings.add ("Education", "education"); SqlBulkCopy.                Columnmappings.add ("Major", "Major"); SqlBulkCopy.                Columnmappings.add ("Stuorigin", "Stuorigin"); SqlBulkCopy.                Columnmappings.add ("Gender", "Gender"); SqlBulkCopy.                Columnmappings.add ("Destination", "Destination"); SqlBulkCopy.                Columnmappings.add ("Hometel", "Hometel"); SqlBulkCopy.                Columnmappings.add ("Tel", "tel"); SqlBulkCopy.                Columnmappings.add ("Emai", "Emai"); SqlBulkCopy.                Columnmappings.add ("StudentID", "StudentID"); SqlBulkCopy.                Columnmappings.add ("School", "School"); SqlBulkCopy. ColumnmaPpings.                ADD ("Gradyear", "gradyear"); SqlBulkCopy.                  Columnmappings.add ("Identityid", "Identityid"); SqlBulkCopy.                WriteToServer (NEWDT); return Content ("Upload successful!            ");  }

Using SqlBulkCopy to implement an Excel bulk Import database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.