Solved

read DBF file

Posted on 2006-07-12
6
1,077 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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