Solved

read DBF file

Posted on 2006-07-12
6
1,035 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:dkloeck
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 17090519
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);
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17090544
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 ^_^
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 500 total points
ID: 17091389
I was just suggesting opening it in FoxPro Excel to get the column names, not to process the data, but you can do it programatically as well:

OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft dBASE Driver (*.dbf);Dbq=c:\mydbpath;");
OdbcCommand command = new OdbcCommand("SELECT * FROM tablename",conn);
DataSet ds = new DataSet();
OdbcDataAdapter da = new OdbcDataAdapter(command);
da.Fill(ds);

foreach(DataColumn column in da.Tables[0].Columns)
{
  Console.Writeline("{0} : {1}",column.Name, column.DataType.ToString());
}
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Author Comment

by:dkloeck
ID: 17100337
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"

0
 
LVL 10

Author Comment

by:dkloeck
ID: 17100363
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?
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17100412
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);


0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now