我能够与Visual Fox Pro数据库进行数据连接,从中我需要2个表。如何加入2个表然后在C#中检索数据?
答案 0 :(得分:3)
首先,我会download Microsoft's 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;
}
}
答案 1 :(得分: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);
}
}
}