码迷,mamicode.com
首页 > 数据库 > 详细

读取DBF文件的部分代码

时间:2014-09-04 16:41:29      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:des   blog   os   ar   for   文件   div   代码   sp   

        private void BtnOpenInitial_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            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>
        /// 从硬盘读取dbf文件
        /// </summary>
        /// <param name="filepath">路径</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>
        /// 读取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>
        /// 读取记录
        /// </summary>
        /// <param name="bReader"></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;
            }
        }

 

读取DBF文件的部分代码

标签:des   blog   os   ar   for   文件   div   代码   sp   

原文地址:http://www.cnblogs.com/len0031/p/3956149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!