开发者

How do I read a Foxpro 8.0 database from c#?

I need to import tables from foxpro 8.0 to sql server. How do I read the tables & schema from a foxpro director开发者_JS百科y/files in C# so I can create the tables in SQL Server and copy the data over?


You can accomplish this through the use of the GetSchema method on the OleDb.Connection class.

OleDbConnection connection = new OleDbConnection(
    "Provider=VFPOLEDB.1;Data Source=.\\Northwind\\Northwind.dbc;"
);
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()
        );
    }
}


You can use ODBCConnection. I know foxpro is using .dbf files.

OdbcConnection Conn = new OdbcConnection("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=C:\\tbl.dbf;");
        String SQL = "SELECT * FROM tbl.dbf";
        Conn.Open();
        OdbcCommand MyCommand = new OdbcCommand(SQL,Conn);
        OdbcDataReader dr = MyCommand.ExecuteReader();
        while (dr.Read())
        {
           //your code
        }


With exception of the "Driver" providd by RJ's answer, go to MS and get the VFP OleDB provider... You might have compatibility issues if dealing with a database container.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜