C#对arcgis SHP文件属性表dbf的操作

本文转自http://www.cnblogs.com/xiexiaokui/archive/2007/11/12/956984.html


背景知识:
arcgis的属性表是dbf格式的,应该是dbf4版本。
dbf可以用excel,access打开读取,但是好像不能进行修改。
因此dbf最好还是用foxpro等数据库系列软件打开。
为了批量处理,尤其是逻辑操作,更希望可以用c#进行操作,
资料一:
参考 http://www.cnblogs.com/tanjy/archive/2006/11/14/560046.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 所对应的表名必须要有路径。

资料二:
参考 http://blog.ouc.edu.cn/2006/07/cdbf-1.html
c#访问dbf数据库方式大全(验证版)
一,通过Visual FoxPro Ole DBProvider,参考http://fox.wikis.com/wc.dll?Wiki~VFPOleDBProvider~VFP 安装一个驱动先,然后作了个连结字,然后ok。
using System;
using System.Data;
using System.Data.ProviderBase;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

OleDbConnection ContactMgmt = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=C:\\文件夹名字;Collating Sequence=MACHINE");
OleDbCommand cmdNewID = new OleDbCommand("select * from dfb的名字", ContactMgmt);
ContactMgmt.Open();
OleDbDataReader aReader = cmdNewID.ExecuteReader();
Response.Write("This is the returned data from test table");
while(aReader.Read())
{
Response.Write("
");
Response.Write(aReader["列名"].ToString());
}
aReader.Close();
ContactMgmt.Close();
}
}

2,如果可以确定哪种dbf格式,可以不用安装驱动,直接用
OleDbConnection ContactMgmt = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\文件夹;Extended Properties=dBASE IV;User ID=Admin;Password=");

3,如果单个的dbf文件也可以使用,这种最简单,应该可以使用,没有测试。嘿嘿。
using Microsoft.Data.Odbc

再用以下代码建立OdbcConnection, OdbcDataAdapter, DataSet.

OdbcConnection oCn = New OdbcConnection("DSN=MYDBF")
OdbcDataAdapter oDa = new OdbcDataAdapter("SELECT * FROM c:\myTable.dbf", oCn)

DataSet oDs = New DataSet()
oDa.Fill(oDs, "myTable")

资料三
参考 http://hi.baidu.com/lostleaf/blog/item/7484b63e75a34efa828b1397.html
c#读取dbf文件2006年12月25日 星期一 16:14此方式将dbf文件所在目录看作数据库,其中的dbf文件看作数据库表,表名即文件名(不加扩展名)。 
 

//把打开的dbf文件导入DATASET

       private DataSet importDbfToDataSet(string FilePath,string tabname)

       {

            string strConnection = @"Dsn=Visual FoxPro Tables;sourcedb=" + FilePath.Substring(0, FilePath.LastIndexOf("\\")) + ";sourcetype=DBF;exclusive=No;backgroundfetch=Yes;collate=Machine";

//对于连接串,注意版本问题

           string   strSelect="SELECT * FROM  "+tabname;  

           OdbcConnection   thisConnection   =   new   OdbcConnection(strConnection);  

           thisConnection.Open();  

           OdbcDataAdapter   thisAdapter   =   new OdbcDataAdapter(strSelect,thisConnection);  

           DataSet   thisDataSet   =   new   DataSet();  

           try  

           {  

              thisAdapter.Fill(thisDataSet);  

           }  

           catch(Exception   exec)  

           {  

              MessageBox.Show(exec.Message);  

           }  

           return thisDataSet;

       }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值