Solved

read DBF file

Posted on 2006-07-12
6
1,057 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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