Search code examples
databaseprimary-keyfoxprovisual-foxpro

Listing primary keys in Fox Pro with the OLE DB Provider?


How does one list the primary keys of a table in Fox Pro with the OLE DB Provider?

Using C# and switching my build to x86 instead of x64 I was able to use oledb provider for Fox Pro to display some information about the table:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace obtainFoxSchema
{
    class Program
    {
        static void Main(string[] args)
        {
            OleDbConnection connection = new OleDbConnection(
                "Provider=VFPOLEDB.1;Data Source=X:\\FREETABLES\\DATA;"
            );
            connection.Open();
            DataTable tables = connection.GetSchema(
                System.Data.OleDb.OleDbMetaDataCollectionNames.Tables
            );

            foreach (System.Data.DataRow rowTables in tables.Rows)
            {
                Console.Out.WriteLine(rowTables["table_name"].ToString());
                DataTable columns = connection.GetSchema(
                    System.Data.OleDb.OleDbMetaDataCollectionNames.Columns,
                    new String[] { null, null, rowTables["table_name"].ToString(), null }
                );
                foreach (System.Data.DataRow rowColumns in columns.Rows)
                {
                    Console.Out.WriteLine(
                        rowTables["table_name"].ToString() + "." +
                        rowColumns["column_name"].ToString() + " = " +
                        rowColumns["data_type"].ToString()
                    );
                }
            }
            Console.Out.WriteLine("stop");
        }
    }
}

But I really don't know where to look in the objects returned to find the primary keys or the foreign keys between tables.

Any ideas where that kind of thing might be?

And yes I found the code for this in another question, but I built the connection string myself using Excel 2007's Get External Data.


Solution

  • I saw your post on Twitter. I am answering this question as a VFP developer with very little experience in Visual Studio and C#.

    Not sure if you need things programmatically or just need to get the details. Have you tried to add the database to the Server Explorer? According to the Visual FoxPro Help File...

    To connect to a Visual FoxPro database or table through the Visual FoxPro OLE DB Provider Open Visual Studio.

    1. From the View menu, select Server Explorer.
    2. In the Server Explorer pane, right-click Data Connections, and click Add Connection
    3. In the Data Link Properties dialog box, click the Provider tab.
    4. Select Microsoft OLE DB Provider for Visual FoxPro.
    5. The Connection tab in the Data Link Properties dialog box appears.

    If this is not helpful, there is a DDEX provider, but it might only work with a specific version of Visual Studio.

    http://vfpx.codeplex.com/wikipage?title=Sedna&referringTitle=Home#DDEX

    If you are using the Entity Framework, there is a provider:

    http://vfpefprovider.codeplex.com/

    Rick