C # read execl and Access databases

Source: Internet
Author: User

First write, please advise!! Words not more say go directly code!

1         /// <summary>2         ///Open File3         /// </summary>4         /// <param name= "Sender" ></param>5         /// <param name= "E" ></param>6         Private voidBtn_file_click (Objectsender, EventArgs e)7         {8OpenFileDialog OpenFile =NewOpenFileDialog ();9Openfile.filter ="execl (*.xls) |*.xls|accdb (*.accdb) |*.accdb";Ten             if(Openfile.showdialog () = =DialogResult.OK) { One Selectinfo (openfile.filename); A             } -}
1 /// <summary>2         ///reading Data3         /// </summary>4         /// <param name= "path" ></param>5          Public voidSelectinfo (stringpath) {6             stringSuffix =path.getextension (Path);7             //string tabname = "Student";8             stringTabName ="";//the table name of the query suggests adding a TextBox control to the form9             if(Tabname.equals (""))Ten             { OneTabName + ="[sheet1$]"; A             } -             stringstrconn =string. Empty; -             //path = environment.currentdirectory; the             Switch(Suffix) -             { -                  Case ". xls": -strconn ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ Path +"; Extended properties= ' Excel 8.0; hdr=no;imex=1; '"; +                     //tabname + = "$"; -                      Break; +                  Case ". xlsx": Astrconn ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ Path +"; Extended properties= ' Excel 8.0; Hdr=false;imex=1 '"; at                    //tabname + = "$"; -                      Break; -                  Case ". mdb": -strconn ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+path; -                      Break; -                  Case ". accdb"://Access2007 instrconn ="Provider=microsoft.ace.oledb.12.0;data source="+path; -                      Break; to                 default: +                      Break; -             } theOleDbConnection oledbconn =NewOleDbConnection (strconn); *             Try $             {Panax Notoginseng              //Oledbconn.open (); -                 stringsql ="Select * from"+tabname; theOleDbDataAdapter Oledbda =NewOleDbDataAdapter (SQL, oledbconn); +DataSet ds =NewDataSet (); AOledbda.fill (DS,"Information"); the                  This. Dgv_info.datasource = ds. tables["Information"]; +             } -             Catch(Exception) $             { $                  -                 Throw; -             } the             finally { - oledbconn.close ();Wuyi             } the}

Operating effect:

Suitable for beginners. First write, God do not spit groove ah ....

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.