使用 C# 从 Visual Fox Pro 数据库中读取数据

Joh*_*wan 1 .net c# visual-foxpro

我能够建立到 Visual Fox Pro 数据库的数据连接,从中我需要 2 个表。如何加入 2 个表然后在 C# 中检索数据?

DRa*_*app 5

首先,我会下载 Microsoft 的 Visual FoxPro OleDb provider

下载并安装后,您可以使用它连接到数据库表所在的 PATH。此外,如果应用程序使用表正确链接的“数据库”,您也可以明确包含数据库名称。

using System.Data;
using System.Data.OleDb;

public class YourClass
{
   public DataTable GetYourData()
   {
      DataTable YourResultSet = new DataTable();

      OleDbConnection yourConnectionHandler = new OleDbConnection(
          "Provider=VFPOLEDB.1;Data Source=C:\\SomePath\\;" );

      // if including the full dbc (database container) reference, just tack that on
//      OleDbConnection yourConnectionHandler = new OleDbConnection(
//          "Provider=VFPOLEDB.1;Data Source=C:\\SomePath\\NameOfYour.dbc;" );


      // Open the connection, and if open successfully, you can try to query it
      yourConnectionHandler.Open();

      if( yourConnectionHandler.State == ConnectionState.Open )
      {
         OleDbDataAdapter DA = new OleDbDataAdapter();
         string mySQL = "select A1.*, B1.* "
                     + " from FirstTable A1 "
                     + "      join SecondTable B1 "
                     + "         on A1.SomeKey = B1.SomeKey "
                     + " where A1.WhateverCondition ";  // blah blah...

         OleDbCommand MyQuery = new OleDbCommand( mySQL, yourConnectionHandle );

         DA.SelectCommand = MyQuery;

         DA.Fill( YourResultSet );

         yourConnectionHandle.Close();
      }

      return YourResultSet;
   }
}
Run Code Online (Sandbox Code Playgroud)