[Mono-list] Re: Direct DBF Access?
Abe Gillespie
abe.gillespie at gmail.com
Wed Nov 30 19:50:53 EST 2005
I couldn't find what I wanted so I created my own ... this code relies
heavily on the monoGIS project. Depends on the ShapeLib library. No
warranty, blah, blah. Also, no comments. :) Enjoy.
-Abe
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
public enum DbfFieldType
{
String,
Integer,
Double,
Logical,
Invalid
};
public class CShapeLib
{
[DllImport("shapelib.dll", CharSet=CharSet.Auto)]
public static extern IntPtr DBFOpen(string szDBFFile, string szAccess);
[DllImport("shapelib.dll", CharSet=CharSet.Auto)]
public static extern int DBFGetFieldCount(IntPtr hDBF);
[DllImport("shapelib.dll", CharSet=CharSet.Auto)]
public static extern DbfFieldType DBFGetFieldInfo(IntPtr hDBF, int iField,
StringBuilder pszFieldName, ref int pnWidth, ref int pnDecimals);
[DllImport("shapelib.dll", CharSet=CharSet.Auto)]
public static extern void DBFClose(IntPtr hDBF);
}
public class CDbfInfo : IDisposable
{
private IntPtr m_hDbf = IntPtr.Zero;
private IntPtr dbfHandle {get {return m_hDbf;}}
private int m_fldCnt;
public int fieldCount {get {return m_fldCnt;}}
public CDbfInfo(FileInfo dbfFileInfo)
{
m_hDbf = CShapeLib.DBFOpen(dbfFileInfo.FullName, "rb");
m_fldCnt = CShapeLib.DBFGetFieldCount(dbfHandle);
}
public void Dispose()
{
CShapeLib.DBFClose(dbfHandle);
}
public CDbfField getField(int index)
{
if (index < 0 || index >= fieldCount)
throw new IndexOutOfRangeException();
return new CDbfField(dbfHandle, index);
}
}
public class CDbfField
{
internal CDbfField(IntPtr hDbf, int index)
{
StringBuilder sb = new StringBuilder(20);
int w = -1;
int decs = -1;
DbfFieldType t = CShapeLib.DBFGetFieldInfo(hDbf, index, sb, ref w, ref decs);
m_type = t;
m_idx = index;
m_name = sb.ToString();
m_w = w;
m_decs = decs;
}
private DbfFieldType m_type;
public DbfFieldType type {get {return m_type;}}
private int m_idx;
public int index {get {return m_idx;}}
private string m_name;
public string name {get {return m_name;}}
private int m_w;
public int width {get {return m_w;}}
private int m_decs;
public int decimals {get {return m_decs;}}
}
On 11/30/05, Abe Gillespie <abe.gillespie at gmail.com> wrote:
> Anyone have a way to access DBF files w/o going through ADO.NET (and
> as far as I can tell this option's only available on Windows going
> through an ODBC connection)? Specifically, I only really need header
> access to inspect the table structure.
>
> There is this:
> http://www.c-sharpcorner.com/Code/2002/Feb/xBaseEngineRFV.asp
>
> But it's managed C++. I guess, theoretically I could compile it in
> Windows and use it in Linux ... but I'd rather not if there's another
> option.
>
> Thanks for the help!
> -Abe
>
More information about the Mono-list
mailing list