Partial code to read the DBF file

Source: Internet
Author: User

        private void Btnopeninitial_click (object sender, EventArgs e) {OpenFileDialog file = new Openfi            Ledialog (); if (file. ShowDialog () = = System.Windows.Forms.DialogResult.OK) {string path = file.                FileName;                dtable dbf = new dtable (); Dbf.                Load (path); if (DBF. Table.Rows.Count > 0) {this. Grcinitialdata.datasource = dbf.                    Table;                    _table = new DataTable (); _table = dbf.                Table; }}}///<summary>///read DBF file from HDD///</summary>//<param name= "fi            Lepath "> Path </param>//<returns></returns> public int Load (String filepath) {            FileInfo ffile = new FileInfo (filepath); if (ffile.exists) {Stream fstream = new BufferedStream (new FileStream (filepath, FileMode.Open)) ;//main. dbf try {binaryreader breader = new BinaryReader (fstream);                    Bytedump (FStream);                Return readdbf (Breader);                    } catch {cleardatatable ();                return-1;                } finally {fstream.close ();            }} else {return-1; }}///<summary>//Read DBF///</summary>//<param name= "Breader" >            </param>//<returns></returns> private int readdbf (BinaryReader breader) {                    if (Readheader (breader) = = 0) {if (Readfielddesc (breader) = = 0) {                Return Readrecord (Breader);            } else return-1;            }else {return-1; }}///<summary>///Read record///</summary>//<param name= "Breader" >&            lt;/param>//<returns></returns> private int Readrecord (BinaryReader breader) { try {int ireccount = _hearder.                RecordCount;                    for (int i = 0; i < Ireccount; i++) {Drow row = new Drow (); DataRow datarow = _table.                    NewRow (); Row.                    RowIndex = i;                    byte Brecordflag = Breader.readbyte (); if (Brecordflag = = 0x20) {row.                    Deleteflag = false; } else if (Brecordflag = = 0x2a) {row.                    Deleteflag = true;       } else {cleardatatable ();                 return-1; } for (Short j = 0; J < _columns. ColumnCount; J + +) {dcolumn col = _columns.                        Getcolumnbyindex (j);                            if (col! = null) {Dfield field = new Dfield (); Field.                            Column = col; byte[] BValue = breader.readbytes (col.                            Columnlength); Field.                            Fieldvalue = Encoding.Default.GetString (BValue); Row.                            AddField (field);                        Updaterowfield (DataRow, field);                        } else {return-1; }} _rows.                    AddRow (row); if (!row. Deleteflag) _table.                Rows.Add (DataRow); }//if (_hearder. RecordCount > 0)                {//Byte Endflag = Breader.readbyte ();                if (endflag! = 0x1A)//{//cleardatatable ();                return-1;            }//} return 0;                } catch {cleardatatable ();            return-1; }        }

Partial code to read the DBF file

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.