Xcode reads Excel Data (applicable to any database)

Source: Internet
Author: User

Although it is a congestion model, although it is a strong type, xcode also supports traversing any database structure and reading data in a strong type (compared to dictionary access such as dataset) mode.

It is easy to traverse the database structure, and most ORM supports it.

However, it is a little complicated to read data in a strongly typed manner without entity classes. The principle of xcode is to dynamically create an entity class inherited from entity <> for each table, and then operate the entity class through the interface.

CodeThere is a fast reflection, although there is no performance loss, but it is quite awkward to use. Here it shows that xcode does not support dynamic access to entity-class metadata.

 

 // Add a connection  Dal . Addconnstr ( "Test" , "Provider = Microsoft. Jet. oledb.4.0 ;" + "Data source=test.xls ;" + "Extended properties = Excel 8.0 ;" , Null , Null ); Dal Dal = Dal . Create ( "Test" ); // Traverse all tables  Foreach ( Xtable Table In Dal. Tables ){ Console . Writeline ( "Table {0 }:" , Table. Name ); // Create an entity operator. An entity class is dynamically generated for the data table and compiled using codedom.  Ientityoperate OP = Dal. createoperate (table. Name. Replace ( "$" ,Null )); // Because of the dynamic code generation defect, $ in the table name has been removed, and the Excel query must always add brackets to the table name, or is there $ // below to set meta through quick reflection. tablename  Type Type = op. GetType (); type = Typeof ( Entity <>. Meta ). Makegenerictype (type ); Propertyinfox . Create (type, "Tablename" ). Setvalue ( "[" + Table. Name + "]" ); // Skip if no record exists If (Op. findcount () <1) Continue ; // Output Header  Foreach ( Fielditem Item In Op. Fields ){ If (Item. Name. startswith ( "F" )) Break ; Console . Write ( "{0} \ t" , Item. Name );} Console . Writeline ();// Search for all data  Entitylist < Ientity > List = op. findall (); // Dataset DS = List. todataset (); // output data  Foreach ( Ientity Entity In List ){ Foreach ( Fielditem Item In Op. Fields ){ If (Item. Name. startswith ( "F" ))Break ; Console . Write ( "{0} \ t" , Entity [item. Name]);} Console . Writeline ();}}

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.