C# 2010, Read from .DBF File, Insert Into DGV

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ive been checking through the forums, so far this is as close to an understandable bit of code I could rig up for this. Ive got a few .DBF files (they display their extension all caps, not sure if its relevant). End goal, there are several
of the .DBF files that I want to read from and just stick in a few DGVs on the form. The "Insert" statement points to a table on an Access 2003 database, burton (I was hoping to just transfer to that and save but no such luck).
Code Chunk:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; void ReadDBF()
{
<span style="color:Blue; string message = <span style="color:Blue; string.Empty;
<span style="color:Blue; string dirPath = <span style="color:#A31515; @"C:Documents and SettingsUserDesktopDBFs";
<span style="color:Blue; try
{
<span style="color:Blue; foreach (<span style="color:Blue; string filePath <span style="color:Blue; in System.IO.Directory.GetFiles(dirPath))
{
<span style="color:Green; // Building SQL String from the files
<span style="color:Blue; string sqlQuery = <span style="color:#A31515; "SELECT ITEM, CLASS, DESCRIP, COST, ONHAND, ONORDER, ALOC, RICE, PRICE2, PRICE3, PRICE4 FROM " + System.IO.Path.GetFileNameWithoutExtension(filePath).ToString();
message += <span style="color:#A31515; "Query" + <span style="color:#A31515; n;
<span style="color:Green; // Insert into new DB file
DataTable dtB = burtuckyDataSet.burton;
message += <span style="color:#A31515; "dtB" + <span style="color:#A31515; n;
OdbcCommand oCom = <span style="color:Blue; new OdbcCommand();
oCom.CommandText = <span style="color:#A31515; "INSERT INTO burton(ITEM, CLASS, DESCRIP, COST, ONHAND, ONORDER, ALOC, RICE, PRICE2, PRICE3, PRICE4) VALUES(@ITEM, @CLASS, @DESCRIP, @COST, @ONHAND, @ONORDER, @ALOC, @RICE, @PRICE2, @PRICE3, @PRICE4)";
message += <span style="color:#A31515; "Insert" + <span style="color:#A31515; n;
<span style="color:Blue; int effected = oCom.ExecuteNonQuery();
message += <span style="color:#A31515; "NonQuery" + <span style="color:#A31515; n;
<span style="color:Green; // Code Here
richTextBox1.AppendText(<span style="color:#A31515; "That worked");

DataTable dt = <span style="color:Blue; new DataTable();
}
}


<span style="color:Blue; catch
{
MessageBox.Show(message, <span style="color:#A31515; "Error", MessageBoxButtons.OK);
}
}
[/code]
<br/>
<br/>
It errors out, getting as far as "Query, dtB, Insert". So my guess is I screwed up on line1 and it just wants to confuse me. Can anybody help me out on this one? Ive already stared at the code enough for it to blur so Im lost.
Thanks in advance! <hr class="sig May the fleas of a thousand camels feast happily on the lower regions of your enemies. And may their arms be too short to scratch!

View the full article
 
Back
Top