Link to home
Start Free TrialLog in
Avatar of dkloeck
dkloeckFlag for Spain

asked on

read DBF file

hi there,

I need to read information from a .dbf file which is part of a shapefile (http://es.wikipedia.org/wiki/Shapefile)
to populate an information window.

Anyone knows how to do it?
Avatar of dstanley9
dstanley9

Do you know the structure of the DBF file?  If not, open it in FoxPro or Excel to get the column names.  Then, you can access it just like any other database using ODBC:

OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft dBASE Driver (*.dbf);Dbq=c:\mydbpath;");
OdbcCommand command = new OdbcCommand("SELECT * FROM tablename",conn);
Avatar of dkloeck

ASKER

i have to do it programatically, so i can not open anything with other programs.

Isn't there a way to read the colums just using c# methods?
or just parsting the file?

btw. thanks for the fast answer ^_^
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkloeck

ASKER

I used following code:
try
{                        
      string connectionString = "Driver={Microsoft dBASE Driver (*.dbf)};DBQ=C:\\gm.dbf";
      OdbcConnection conn = new OdbcConnection(connectionString);
      OdbcCommand command = new OdbcCommand("SELECT * FROM tablename",conn);
      DataSet ds = new DataSet();
      OdbcDataAdapter da = new OdbcDataAdapter(command);
      da.Fill(ds);

      foreach(DataColumn ncolumn in ds.Tables[0].Columns)
      {
            textBox1.Text+=ncolumn.ColumnName.ToString()+" : "+ncolumn.DataType.ToString();
      }
}
catch(Exception ex)
{
      MessageBox.Show(ex.ToString());
}

and i get following error:
"System.Data.Odbc.OdbcException: ERROR [HY024] [Microsoft][ODBC dBASE Driver] '(unbekannt)' ist kein zulässiger Pfad. Stellen Sie sicher, dass der Pfad richtig eingegeben wurde und dass Sie mit dem Server, auf dem sich die Datei befindet, verbunden sind.
ERROR [IM006] [Microsoft][ODBC Driver Manager] Fehler bei SQLSetConnectAttr-Aufruf für Treiber
ERROR [HY024] [Microsoft][ODBC dBASE Driver] '(unbekannt)' ist kein zulässiger Pfad. Stellen Sie sicher, dass der Pfad richtig eingegeben wurde und dass Sie mit dem Server, auf dem sich die Datei befindet, verbunden sind.
   at System.Data.Odbc.OdbcConnection.Open()
   at System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState)
   at System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
   at DBFReader.Form1.button1_Click(Object sender, EventArgs e) in f:\software dani\visual studio projects\tests\ziptest\dbfreader\form1.cs:line 111"

Avatar of dkloeck

ASKER

it seems that the path is wrong.. ("DBQ=C:\\gm.dbf")
is it not right to put the path to the file I want to open there?
Put the directory in the connection string, not the file name.  The file name will be specified in the SELECT query.

     string connectionString = "Driver={Microsoft dBASE Driver (*.dbf)};DBQ=C:\\";
     OdbcConnection conn = new OdbcConnection(connectionString);
     OdbcCommand command = new OdbcCommand("SELECT * FROM gm",conn);