Search code examples
c#sqlodbcoledbvisual-foxpro

Speed problems in getting Schema dynamically from a visual foxpro Datasource


Having problems with Visual Fox Pro ODBC drivers (and OLE DB) the code I'm using to dynamically get all the column names for a DBF table works fine for small datasets. But if I try getting the table schema using the below code for a large table then it can take 60 seconds to get the schema.

OdbcConnection conn = new OdbcConnection(@"Dsn=Boss;sourcedb=u:\32BITBOSS\DATA;sourcetype=DBF;exclusive=No;backgroundfetch=Yes;collate=Machine;null=Yes;deleted=Yes");     
OdbcCommand cmd = new OdbcCommand("Select * from " + listBox1.SelectedItem.ToString(), conn);
conn.Open();
try
{

    OdbcDataReader reader = cmd.ExecuteReader();
    DataTable dt = reader.GetSchemaTable();

    listBox2.Items.Clear();

    foreach (DataRow row in dt.Rows)
    {
        listBox2.Items.Add(row[0].ToString());
    }

    conn.Close();

}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
    conn.Close();
}

Now I get that this is due to the Select command dragging all 500,000 records or so into the data reader before it can then extract the column names. But visual fox pro is pretty rubbish at limiting the record return.

I ideally want to limit the return to just 1 record, but on the assumption I don't know any of the column names to utilise a WHERE clause this doesn't work.

VFP can utilise the TOP SQL command, but to do that you need an ORDER BY to go with it, and since I don't have the any of the column names to utilise, this doesn't work either.

So am a bit stumped trying to think of a clever way to dynamically get the table schema without the slow down in VFP.

I don't think database conversions on the fly will be any faster. Anyone got any clever ideas (apart from changing the database system, it's inherited in the job and I currently have to work with it :) )?


Solution

  • Change this line:

    OdbcCommand cmd = new OdbcCommand("Select * from " + listBox1.SelectedItem.ToString(), conn); 
    

    To this line:

    OdbcCommand cmd = new OdbcCommand(string.Format("Select * from {0} where 1 = 0", listBox1.SelectedItem.ToString()), conn); 
    

    This will bring back zero records, but fill the schema. Now that you have the schema you can build a filter further.