使用 C# 从 Visual Fox Pro 数据库中读取数据
本文关键字:数据库 读取 数据 Pro Fox Visual 使用 | 更新日期: 2023-09-27 18:34:47
我可以与Visual Fox Pro数据库建立数据连接,从它我需要2个表。如何连接 2 个表然后在 C# 中检索数据?
首先,我会下载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;
}
}
以防有人正在寻找一个有点完整的测试。 您需要从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);
}
}
}