1

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

4

2 回答 2

5

首先,我会下载 Microsoft 的 Visual FoxPro OleDb 提供程序

下载并安装后,您可以使用它连接到数据库表所在的 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;
   }
}
于 2012-05-22T17:56:38.183 回答
0

如果有人正在寻找一个比较完整的测试。您需要从 Microsoft 下载 VFPOLEDB 驱动程序。

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

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                OleDbConnection connection = new OleDbConnection(
        "Provider=VFPOLEDB.1;Data Source=F:\\Gutters\\Data\\database.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()
                        );
                    }
                }

                string sql = "select * from customer";
                OleDbCommand cmd = new OleDbCommand(sql, connection);

                DataTable YourResultSet = new DataTable();
                OleDbDataAdapter DA = new OleDbDataAdapter(cmd);

                DA.Fill(YourResultSet);
            }
        }
    }
于 2014-03-13T02:49:29.980 回答