读取SHP文件中的dbf表
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data;
5 using System.IO;
6
7 namespace ReadDbfTest
8 {
9 /// <summary>
10 /// 一个代表DBF文件的类。
11 /// editted by ice
12 /// hbhbice#gmail.com(@)
13 /// </summary>
14 public class DBaseFile
15 {
16 #region Private Members
17
18 /// <summary>
19 /// 文件版本
20 /// </summary>
21 private string _Version;
22 /// <summary>
23 /// 文件修改日期
24 /// </summary>
25 private string _ModifyDate;
26 /// <summary>
27 /// 记录条数
28 /// </summary>
29 private int _RecordCount;
30 /// <summary>
31 /// 文件头字节数
32 /// </summary>
33 private Int16 _FileHeadLength;
34 /// <summary>
35 /// 一条记录的字节长度
36 /// </summary>
37 private Int16 _RecordLength;
38 /// <summary>
39 /// 文件的记录数据
40 /// </summary>
41 private DataTable _Table;
42 /// <summary>
43 /// Language Drive ID 我也没有查到它到底是做什么的
44 /// </summary>
45 private int _LanguageDriveID;
46 #endregion
47
48
49 #region Property
50
51 /// <summary>
52 /// 文件版本
53 /// </summary>
54 public string Version
55 {
56 get
57 {
58 return _Version;
59 }
60 set
61 {
62 _Version = value;
63 }
64 }
65
66 /// <summary>
67 /// 文件修改日期
68 /// </summary>
69 public string ModifyDate
70 {
71 get
72 {
73 return _ModifyDate;//throw new System.NotImplementedException();
74 }
75 set
76 {
77 _ModifyDate = value;
78 }
79 }
80
81 /// <summary>
82 /// 记录条数
83 /// </summary>
84 public int RecordCount
85 {
86 get
87 {
88 return _RecordCount;//throw new System.NotImplementedException();
89 }
90 set
91 {
92 _RecordCount = value;
93 }
94 }
95
96 /// <summary>
97 /// 一条记录的字节长度
98 /// </summary>
99 public short RecordLength
100 {
101 get
102 {
103 return _RecordLength;//throw new System.NotImplementedException();
104 }
105 set
106 {
107 _RecordLength = value;
108 }
109 }
110
111 /// <summary>
112 /// 文件的记录数据
113 /// </summary>
114 public DataTable Table
115 {
116 get
117 {
118 return _Table; //throw new System.NotImplementedException();
119 }
120 set
121 {
122 _Table = value;
123 }
124 }
125
126
127 #endregion
128
129 public DBaseFile()
130 {
131 this._Table = new DataTable();
132 }
133 /// <summary>
134 /// 将文件的数据读到类中
135 /// </summary>
136 ///
137 public void Read(string FileName)
138 {
139 try
140 {
141 FileStream fs = new FileStream (FileName ,FileMode.Open );
142 BinaryReader br = new BinaryReader (fs);
143 #region Read File Head
144
145 Version = br.ReadByte().ToString();
146 byte yy = br.ReadByte();
147 byte mm = br.ReadByte();
148 byte dd = br.ReadByte();
149 ModifyDate = ((int)yy + 1900).ToString() + " " + mm.ToString() + " " + dd.ToString();
150 RecordCount = br.ReadInt32();
151 _FileHeadLength = br.ReadInt16();
152 RecordLength = br.ReadInt16();
153
154 for (int i = 0; i < 17; i++)
155 {
156 byte temp1 = br.ReadByte();
157 } //读了29个字节,因为从1开始,所以下一个为整个文件的第29个字节
158 //以下为文件的第29个字节
159 _LanguageDriveID =(int) br.ReadByte();
160
161 br.ReadByte();
162 br.ReadByte(); //读完32个字节,以下便 是记录的描述了。
163
164 int ColumnCount = (_FileHeadLength - 33) / 32;
165
166 int[] RecordItemLength = new int[ColumnCount];
167
168 for (int i = 0; i < ColumnCount ; i++)
169 {
170 string strName = ""; // 11个字节记录项名称,是ASCII码值。
171
172 for (int k = 0; k < 11; k++)
173 {
174 char[] Name = new char[11];
175 Name[k] = (char)br.ReadByte();
176 strName = strName + Name[k];
177 }
178 char DataType = (char)br.ReadByte(); //记录项记录类型
179 int temp2 = br.ReadInt32(); //四个保留字,用0添写
180 RecordItemLength[i] = (int)br.ReadByte(); //记录项长度
181 Int16 RecordItemPrecision = (Int16)br.ReadByte(); //记录项的精度
182 Int16 temp3 = br.ReadInt16(); //2个字节保留字节
183
184 Int16 WorkspaceID = (Int16)br.ReadByte(); //1个字节工作区ID
185
186 for (int j = 0; j < 5; j++) //10个字节保留字节
187
188 {
189 Int16[] temp = new Int16[5];
190 temp[j] = br.ReadInt16();
191 }
192 byte MDXFlag = br.ReadByte(); // 1个字节MDX标识
193 DataColumn dc =null;
194 switch (DataType )
195 {
196
197 case 'C': //字符型 允许输入各种字符
198 dc = new DataColumn(strName);
199 dc.DataType = Type.GetType("System.String");
200 this._Table.Columns.Add(dc);
201 break;
202 case 'N': //数值型(Numeric)
203 dc = new DataColumn (strName );
204 dc.DataType = Type.GetType("System.String");
205 //dc.DataType =Type.GetType ("System.Double" );
206 this._Table.Columns.Add(dc);
207 break;
208 case 'F':
209 dc = new DataColumn(strName);
210 dc.DataType = Type.GetType("System.String");
211 this._Table.Columns.Add(dc);
212 break;
213 case 'D': //日期型 用于区分年、月、日的数字和一个字符,内部存储按照YYYYMMDD格式。
214 throw new NotImplementedException ();
215 case 'G': //General or OLE
216 throw new NotImplementedException();
217
218 case 'B': //二进制 允许输入各种字符
219 throw new NotImplementedException();
220 case 'L':
221 throw new NotImplementedException();
222 case 'M':
223 throw new NotImplementedException();
224 default:
225 throw new NotImplementedException();
226 }
227
228 }
229 char RecordTerminalTag = (char)br.ReadByte(); //1个字节作为记录项终止标识。
230 #endregion
231
232 #region 读记录体
233
234 for (int jj = 0; jj < RecordCount ; jj++)
235 {
236
237 //byte []temp111 = br.ReadBytes (71);
238 char tempchar=(char) br.ReadByte(); //每个记录的开始都有一个空格
239 DataRow dr = _Table.NewRow();
240 for (int i = 0; i < ColumnCount; i++)
241 {
242 byte[] temp = br.ReadBytes(RecordItemLength[i]);
243 //dr[i] = UnicodeEncoding.Unicode.GetString(temp);
244 dr[i] = Encoding.Default.GetString(temp).Trim ();
245 }
246 _Table.Rows.Add(dr);
247 }
248
249 #endregion
250
251
252 while (br.BaseStream.Position < br.BaseStream.Length)
253 {
254 byte b = br.ReadByte();
255 }
256
257 }
258 catch (Exception ex)
259 {
260
261 throw ex;
262 }
263
264 //throw new System.NotImplementedException();
265 }
266 }
267 }
268
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data;
5 using System.IO;
6
7 namespace ReadDbfTest
8 {
9 /// <summary>
10 /// 一个代表DBF文件的类。
11 /// editted by ice
12 /// hbhbice#gmail.com(@)
13 /// </summary>
14 public class DBaseFile
15 {
16 #region Private Members
17
18 /// <summary>
19 /// 文件版本
20 /// </summary>
21 private string _Version;
22 /// <summary>
23 /// 文件修改日期
24 /// </summary>
25 private string _ModifyDate;
26 /// <summary>
27 /// 记录条数
28 /// </summary>
29 private int _RecordCount;
30 /// <summary>
31 /// 文件头字节数
32 /// </summary>
33 private Int16 _FileHeadLength;
34 /// <summary>
35 /// 一条记录的字节长度
36 /// </summary>
37 private Int16 _RecordLength;
38 /// <summary>
39 /// 文件的记录数据
40 /// </summary>
41 private DataTable _Table;
42 /// <summary>
43 /// Language Drive ID 我也没有查到它到底是做什么的
44 /// </summary>
45 private int _LanguageDriveID;
46 #endregion
47
48
49 #region Property
50
51 /// <summary>
52 /// 文件版本
53 /// </summary>
54 public string Version
55 {
56 get
57 {
58 return _Version;
59 }
60 set
61 {
62 _Version = value;
63 }
64 }
65
66 /// <summary>
67 /// 文件修改日期
68 /// </summary>
69 public string ModifyDate
70 {
71 get
72 {
73 return _ModifyDate;//throw new System.NotImplementedException();
74 }
75 set
76 {
77 _ModifyDate = value;
78 }
79 }
80
81 /// <summary>
82 /// 记录条数
83 /// </summary>
84 public int RecordCount
85 {
86 get
87 {
88 return _RecordCount;//throw new System.NotImplementedException();
89 }
90 set
91 {
92 _RecordCount = value;
93 }
94 }
95
96 /// <summary>
97 /// 一条记录的字节长度
98 /// </summary>
99 public short RecordLength
100 {
101 get
102 {
103 return _RecordLength;//throw new System.NotImplementedException();
104 }
105 set
106 {
107 _RecordLength = value;
108 }
109 }
110
111 /// <summary>
112 /// 文件的记录数据
113 /// </summary>
114 public DataTable Table
115 {
116 get
117 {
118 return _Table; //throw new System.NotImplementedException();
119 }
120 set
121 {
122 _Table = value;
123 }
124 }
125
126
127 #endregion
128
129 public DBaseFile()
130 {
131 this._Table = new DataTable();
132 }
133 /// <summary>
134 /// 将文件的数据读到类中
135 /// </summary>
136 ///
137 public void Read(string FileName)
138 {
139 try
140 {
141 FileStream fs = new FileStream (FileName ,FileMode.Open );
142 BinaryReader br = new BinaryReader (fs);
143 #region Read File Head
144
145 Version = br.ReadByte().ToString();
146 byte yy = br.ReadByte();
147 byte mm = br.ReadByte();
148 byte dd = br.ReadByte();
149 ModifyDate = ((int)yy + 1900).ToString() + " " + mm.ToString() + " " + dd.ToString();
150 RecordCount = br.ReadInt32();
151 _FileHeadLength = br.ReadInt16();
152 RecordLength = br.ReadInt16();
153
154 for (int i = 0; i < 17; i++)
155 {
156 byte temp1 = br.ReadByte();
157 } //读了29个字节,因为从1开始,所以下一个为整个文件的第29个字节
158 //以下为文件的第29个字节
159 _LanguageDriveID =(int) br.ReadByte();
160
161 br.ReadByte();
162 br.ReadByte(); //读完32个字节,以下便 是记录的描述了。
163
164 int ColumnCount = (_FileHeadLength - 33) / 32;
165
166 int[] RecordItemLength = new int[ColumnCount];
167
168 for (int i = 0; i < ColumnCount ; i++)
169 {
170 string strName = ""; // 11个字节记录项名称,是ASCII码值。
171
172 for (int k = 0; k < 11; k++)
173 {
174 char[] Name = new char[11];
175 Name[k] = (char)br.ReadByte();
176 strName = strName + Name[k];
177 }
178 char DataType = (char)br.ReadByte(); //记录项记录类型
179 int temp2 = br.ReadInt32(); //四个保留字,用0添写
180 RecordItemLength[i] = (int)br.ReadByte(); //记录项长度
181 Int16 RecordItemPrecision = (Int16)br.ReadByte(); //记录项的精度
182 Int16 temp3 = br.ReadInt16(); //2个字节保留字节
183
184 Int16 WorkspaceID = (Int16)br.ReadByte(); //1个字节工作区ID
185
186 for (int j = 0; j < 5; j++) //10个字节保留字节
187
188 {
189 Int16[] temp = new Int16[5];
190 temp[j] = br.ReadInt16();
191 }
192 byte MDXFlag = br.ReadByte(); // 1个字节MDX标识
193 DataColumn dc =null;
194 switch (DataType )
195 {
196
197 case 'C': //字符型 允许输入各种字符
198 dc = new DataColumn(strName);
199 dc.DataType = Type.GetType("System.String");
200 this._Table.Columns.Add(dc);
201 break;
202 case 'N': //数值型(Numeric)
203 dc = new DataColumn (strName );
204 dc.DataType = Type.GetType("System.String");
205 //dc.DataType =Type.GetType ("System.Double" );
206 this._Table.Columns.Add(dc);
207 break;
208 case 'F':
209 dc = new DataColumn(strName);
210 dc.DataType = Type.GetType("System.String");
211 this._Table.Columns.Add(dc);
212 break;
213 case 'D': //日期型 用于区分年、月、日的数字和一个字符,内部存储按照YYYYMMDD格式。
214 throw new NotImplementedException ();
215 case 'G': //General or OLE
216 throw new NotImplementedException();
217
218 case 'B': //二进制 允许输入各种字符
219 throw new NotImplementedException();
220 case 'L':
221 throw new NotImplementedException();
222 case 'M':
223 throw new NotImplementedException();
224 default:
225 throw new NotImplementedException();
226 }
227
228 }
229 char RecordTerminalTag = (char)br.ReadByte(); //1个字节作为记录项终止标识。
230 #endregion
231
232 #region 读记录体
233
234 for (int jj = 0; jj < RecordCount ; jj++)
235 {
236
237 //byte []temp111 = br.ReadBytes (71);
238 char tempchar=(char) br.ReadByte(); //每个记录的开始都有一个空格
239 DataRow dr = _Table.NewRow();
240 for (int i = 0; i < ColumnCount; i++)
241 {
242 byte[] temp = br.ReadBytes(RecordItemLength[i]);
243 //dr[i] = UnicodeEncoding.Unicode.GetString(temp);
244 dr[i] = Encoding.Default.GetString(temp).Trim ();
245 }
246 _Table.Rows.Add(dr);
247 }
248
249 #endregion
250
251
252 while (br.BaseStream.Position < br.BaseStream.Length)
253 {
254 byte b = br.ReadByte();
255 }
256
257 }
258 catch (Exception ex)
259 {
260
261 throw ex;
262 }
263
264 //throw new System.NotImplementedException();
265 }
266 }
267 }
268