Solved

read DBF file

Posted on 2006-07-12
6
1,041 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

14 Experts available now in Live!

Get 1:1 Help Now