B/S Excel import to SQL Server

Source: Internet
Author: User
Try {string physicspath = "d :\\" + fileupload. filename; // location where the Excel file is stored // generate a string connecting to the Excel Data Table string strodbccon = @ "provider = Microsoft. ace. oledb.12.0; persist Security info = false; Data Source = "+ physicspath +"; extended properties = Excel 8.0 "; // define the oledbconnection object instance and connect to the Excel table oledbconnection oledb = new oledbconnection (strodbccon); // define the oledbdataadapter object instance and call the SELECT query statement to extract the Excel Data Information oledbdataadapter ol Edat = new oledbdataadapter ("select * from [sheet1 $]", oledb); datatable dt = new datatable (); // defines the datatable object instance oledat. fill (DT); // call the fill method // connect to the data table and display the data dataset DS = new dataset (); If (DT. rows. count> 0) {list <tb_rduserinfo> List = new list <tb_rduserinfo> (); // The example written here uses the architecture, but its concept is as follows: first, read the values in Excel and fill them into dataset. Then insert them into the database foreach (datarow DR in DT. rows) {tb_rduserinfo model = new tb_rduserinfo (); Mode L. USERSID = Dr ["no."]. tostring (); Model. usersname = Dr ["name"]. tostring (); Model. loginname = Dr ["Logon username"]. tostring (); string roleid = Dr ["role"]. tostring (); tb_rdroleinfo tb_rdrole_4 = new tb_rdroleinfo (); tb_rdroleservice tb_rdrole_5 = new tb_rdroleservice (); tb_rdrole_4 = role (roleid); Model. roleid = tb_rdrole_4; model. password = Dr ["password"]. tostring (); Model. mobile = Dr ["mobile phone number"]. tostring (); Model. birthday = Dr ["Date of Birth"]. tostring (); Model. sex = Dr ["gender"]. tostring (); Model. dentitycard = Dr ["ID card"]. tostring (); Model. orgname = Dr ["unit name"]. tostring (); Model. deptname = Dr ["department name"]. tostring (); Model. post = Dr ["Job Info"]. tostring (); Model. periodname = Dr [""]. tostring (); Model. email = Dr ["email"]. tostring (); Model. phone = Dr ["landline"]. tostring (); Model. address = Dr ["mailing address"]. tostring (); Model. zipcodei D = Dr ["zip code"]. tostring (); Model. isadmin = Dr ["Administrator"]. tostring (); Model. currentstate = Dr ["current state"]. tostring (); Model. memo = Dr ["Remarks"]. tostring (); string delegationid = Dr [""]. tostring (); tb_delegationinfo pipeline = new tb_delegationinfo (); tb_delegationservice tb_delegation_22 = new tb_delegationservice (); tb_delegation_21 = pipeline (delegationid); Model. delegationid = Tb_delegation_21; model. managework = Dr ["in charge of work"]. tostring (); Model. usertype = Dr ["User Classification"]. tostring (); list. add (model);} usermanager. add (list); MessageBox. show (this, "batch add users successfully! ");} Else {MessageBox. Show (this," Sorry, no data can be added! ") ;}} Catch (exception ey) {MessageBox. Show (this, ey. Message); // prompt dialog box}

Add reference: using system. Data. oledb;

Drag the fileupload control into the page design.

Code Section:

Related Article

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.