c#生成DBF文件,有两种方式,一种是普通的DBF文件,可用Visual Foxpro打开和编辑,但不能用于Arcgis;另一种方法生成的DBF文件,既可用VF打开,又可以作为Arcgis属性表,在Arcgis中打开。
网上的方法往往只是前一种方法,但我们需要的常常是后一种方法。
方法一:
参考来源:http://www.cnblogs.com/xiexiaokui/archive/2007/11/12/956984.html
protected void Page_Load(object sender, EventArgs e) { System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(); string table = @"D:\aaa\code.dbf"; string connStr=@"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); OdbcCommand cmd = new OdbcCommand(); cmd.Connection = conn; string sql = "update " + table + " set other='2',rate=1.014 "; cmd.CommandText = sql; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); sql = @"select * from " + table; OdbcDataAdapter da = new OdbcDataAdapter(sql,conn); DataTable dt = new DataTable(); da.Fill(dt); this.GridView1.DataSource = dt.DefaultView; this.GridView1.DataBind(); } 注意点:1 连接串的格式。2 所对应的表名必须要有路径。
注意:对于日期时间类型数据,CREATE TABLE c:\test.dbf (dt T,id int(10),,price char(10));
插入日期时间类型数据时,insert into c:\test.dbf values({^2013/3/18 22:02:00},1,'12');
另外,字段名长度不能大于10个字符。
方法二:
参考来源:http://www.cnblogs.com/peijian708/archive/2009/06/23/1509401.html
string str_oleConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;Extended Properties=dBASE IV;"; System.Data.OleDb.OleDbConnection ole_conn = new System.Data.OleDb.OleDbConnection(str_oleConn); try { ole_conn.Open(); System.Data.OleDb.OleDbCommand cmd1 = new System.Data.OleDb.OleDbCommand ("Create Table xxx (Field1 int, Field2 char(10))", ole_conn); System.Data.OleDb.OleDbCommand cmd2 = new System.Data.OleDb.OleDbCommand ("Insert Into xxx values (1,'Hello3')", ole_conn); System.Data.OleDb.OleDbCommand cmd3 = new System.Data.OleDb.OleDbCommand ("Insert Into xxx values (2,'Hello4')", ole_conn); cmd1.ExecuteNonQuery(); cmd2.ExecuteNonQuery(); cmd3.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { ole_conn.Close(); }