read dbase(dbf) files...OleDB?
Hey guys, I have some dBase files on my computer that I am trying to read with C#. I found some VB.NET code that I am trying to convert to C#, but I am getting an error that I can't figure out.
on the rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess); line it says I am missing a required Parameter...however, there is only 1 parameter you can put there and that is the correct one.
Is there something else that looks odd or wrong here?
Thanks!:wave:
Code:
//******************
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using MySql.Data.MySqlClient;
using System.Runtime.InteropServices;
using System.Threading;
using System.Data.OleDb;
//************************
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+folderPath+";Extended Properties=dBase IV";
OleDbConnection conn = new OleDbConnection(conString);
OleDbCommand cmd = conn.CreateCommand();
OleDbDataReader rdr;
cmd.CommandText = "SELECT * FROM UF01J1RU.dbf";
conn.Open();
rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
Re: read dbase(dbf) files...OleDB?
Is this a compilation error or a run time exception? What is the EXACT error message (which you should ALWAYS provide)?
Re: read dbase(dbf) files...OleDB?
It is a compilation error..the error says as I specified..but if you want it word for word, "No value given for one or more required parameters."
Thanks!:wave:
Re: read dbase(dbf) files...OleDB?
So you're saying that your project won't run because that code won't compile, and that line of code is underlined and when you mouse over it you get that message? I'm thinking not.
Re: read dbase(dbf) files...OleDB?
Yep, exactly. that line of code turns yellow(Highlighted) and I get the little box pointing to it saying that exact error.
I know it makes no sense...that is why I came here.
I am thinking the compiler is confused and the error is somewhere else..but that is where it showing up.
But it definately has something to do with that section of code.
Re: read dbase(dbf) files...OleDB?
That's a run time exception, NOT a compilation error. It's telling you that the line cannot be successfully EXECUTED, not that it can't be COMPILED. I think you'll find the issue is that your query is dodgy. Try wrapping the file name in square brackets.
Re: read dbase(dbf) files...OleDB?
Yep, sorry about that, it's been a few hours since I was working on it, I was thinking that it gave that error on compile, but yo uare right I just ran it again and it does indeed
happen after it loads. Thanks.
Ok, so anypoo. Now, since this file is on my hard drive and it is just a file (Not a database) I was confused about opening it like a database with a connection string and all that. But it is a dBase file (Similar to MS Access I suppose) so I am wondering about the code I have.
I read that I had to name the file in the SQL statement..but maybe that is not right? Also it said to provide the folder path as the Datasource.
You ever done anything like this? It seems strange to me.
I tried using this "[UF01J1RU.dbf]"..but same error.
Thnks again for the help.
Re: read dbase(dbf) files...OleDB?
Ok, I found some slighty different code and it seems to work fine except for one issue. It disregards the file extension on the file and only looks for .dbf.
For instance, if I have 4 files all named File1 except with different extensions it doesn't work. These are all dbase files, just named with program specific extensions. Each group of files are related to eachother like tables in a database.
I guess me question is..how do I open and read a dbase file that doesn't have a .dbf extension?
For example the folder I am pulling the files from might look like this
File1.env
File1.ptf
File1.lpa
File1.ttp
File2.env
File2.ptf
File2.lpa
File2.ttp
File3.env
File3.ptf
File3.lpa
File3.ttp
Each group of files share the same file name, but have different extensions.
Is there any way I can get this code to actualy open the exact file I specify? Like if I put "SELECT * FROM File1.env" it says that file cannot be found..but if I rename the file to File1.dbf it finds it just fine.
I can even put "SELECT * FROM File1" and as long as there is a File1.dbf in there it will read it, but with any other extension it won't.
I know it is possible to do because I have seen a couple of other programs that do it, but I am at a loss. Maybe I need to use a different driver?
Anyway, here is the code I am using.
Code:
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\EMS FILES\\;Extended Properties=dBase IV";
OleDbConnection conn = new OleDbConnection(conString);
conn.Open();
// create the DataSet
DataSet ds = new DataSet();
// create the adapter and fill the DataSet
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM UF01J1RU.env", conn);
adapter.Fill(ds);
conn.Close();
//spit out data
DataTable dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
MessageBox.Show(dr["LBR_TYPE"].ToString());
}//end for
Thanks!:wave:
Re: read dbase(dbf) files...OleDB?
Never worked with dBase files myself so I can't be of any more help.