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.
It is a runtime error and the exact error is "No value given for one or more required parameters."
Is there something else that looks odd or wrong here? is the connection string wrong? Is the SQL statement causing it? Something else? It is definately happening in the section of code.
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);
[/code]
ArcPosted Jul 10, 2007, 8:17 PM
Hrmmm.
AlanPosted Jul 10, 2007, 7:11 PM
The only suggestion I can come up with is to temporarily rename the files so that they have a .dbf extension (say file1.env becomes file1_env.dbf) and then restore them to their original names before your program ends.
ArcPosted Jul 10, 2007, 4:47 PM
Thanks for the input.
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.
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
[/code]
Thanks!
AlanPosted Jul 10, 2007, 11:24 AM
Try leaving off the .dbf extension:
cmd.CommandText = "SELECT * FROM UF01J1RU";
ArcPosted Jul 10, 2007, 6:27 AM
It compiles just fine..it gives me that error when it gets to that point in runtime.
I am using VS 2005 standard with the lastest updates. Framework Version 2.0.50727. I don't think the Mysql thing is interfering, I have not had errors with that, but who knows.
The ExecuteReader has 1 overload for that function which is what I am using...the default function uses nothing. I get the same error regardless.
It is quite confusing.
Jan MontanoPosted Jul 10, 2007, 6:08 AM
I tried the code and it compiles. Does the namespace MySql.Data.MySqlClient by chance also has an OleDbCommand class? But I think it will result in an ambiguous reference error if ever.
What version of .net framework are you using? I'm using 2.0.50727. It might be that the version you are using right now, has a different implementation. But that's just a guess. If you put on intellisense? does it only show 1 parameter for the ExecuteReader method which is CommandBehavior?