Excel Data Import Database

Source: Internet
Author: User
Tags import database

1 using(FileStream fs = File.openread (@"d:\ performance management \ list. xls"))2             {3Iworkbook wk =NewHssfworkbook (FS);//Create workbook based on file stream4                 if(wk. Numberofsheets >0)5                 {6Isheet sheet = wk. Getsheetat (0);//Create a worksheet7                     //School Number name gender birth month professional8                      for(intR =1; R <= Sheet. Lastrownum; r++)//r=1, read from line 1th, not line No. 0, row No. 0 is column name
9 {Ten #regionGet Excel Data OneIRow row = sheet. GetRow (R); A intStuid; - if(Row. Getcell (0). Celltype==celltype.blank)//PRIMARY key Stuid does not allow null, and if empty, the row data is skipped - { the Continue; - } - Else - { +Stuid = (int) row. Getcell (0). Numericcellvalue; - } + stringname = row. Getcell (1) ==NULL?NULL: Row. Getcell (1). Stringcellvalue; A stringgender = row. Getcell (2)==NULL?NULL: Row. Getcell (2). Stringcellvalue; at stringBirth = row. Getcell (3) ==NULL?NULL: Row. Getcell (3). Stringcellvalue; - stringSpecialty = row. Getcell (4)==NULL?NULL: Row. Getcell (4). Stringcellvalue; - #endregion - - stringsql ="INSERT into Ceshi values (@stuid, @name, @birth, @gender, @specialty)"; -Sqlparameter[] Para =Newsqlparameter[]{ in NewSqlParameter ("@stuid", Stuid), - //Null value is NULL in C #, but inserting a null value into the database through an SQL statement should be DBNull.Value to NewSqlParameter ("@name", name==NULL? DBNull.Value: (Object) name), + NewSqlParameter ("@birth", birth==NULL? DBNull.Value: (Object) birth), - NewSqlParameter ("@gender", gender==NULL? DBNull.Value: (Object) gender), the NewSqlParameter ("@specialty", specialty==NULL? DBNull.Value: (Object) specialty), * }; $ using(cmd =NewSqlCommand (SQL, Getconn ()))Panax Notoginseng { - if(para!=NULL) the { + cmd. Parameters.addrange (para); A } the cmd. ExecuteNonQuery (); + } - } $MessageBox.Show ("Import succeeded"); $ } -}

Excel Data 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.