Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Easy in VFP !!!
Message
From
13/07/2005 12:26:04
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
ASP.NET
Category:
ADO.NET
Title:
Miscellaneous
Thread ID:
01032196
Message ID:
01032287
Views:
34
>Hello there, just been thrown in the deep end with .NET, I need to perform the equivalent of "Set Relation" using CSharp on two Foxpro tables. My basic needs are: Open two Foxpro tables, relate them using two common fields, ( loads of logic goes here... ) create an output table. I know how to open and read them, but am at a loss as to how to create and update a new table
>
>any help much appreciated

Pete,
Which part are you asking? Set relation or creating the table?
I must admit though it's hard at first, ADO.Net's relation support is better than VFP's. Below is a simple relation showing different usage of relation:
using System;
using System.Data;
using System.Data.OleDb;

namespace RelatedDataset
{
  class Class1
  {
   [STAThread]
   static void Main(string[] args)
   {
    string strcon,strsql1,strsql2;
    strcon = @"Provider=VFPOLEDB;
Data Source=C:\Program Files\Microsoft Visual FoxPro 7\Samples\Data\testdata.dbc";
    strsql1 = "select * from customer";
    strsql2 = "select * from orders";

    DataSet ds = new DataSet("testdata");
    ds.Tables.Add("customer");
    ds.Tables.Add("orders");

    OleDbConnection conn = new OleDbConnection(strcon);
    conn.Open();

    OleDbDataAdapter daCustomer = new OleDbDataAdapter(strsql1, conn);
    OleDbDataAdapter daOrders = new OleDbDataAdapter(strsql2, conn);

    daCustomer.Fill(ds.Tables["customer"]);
    daOrders.Fill(ds.Tables["orders"]);
    conn.Close();

/* Relation in .Net is more flexible than
VFP's but that is not obvious here. We're doing a simple relation*/

    DataRelation myRel = new DataRelation("CustomerOrders", 
         ds.Tables["customer"].Columns["cust_id"],
         ds.Tables["orders"].Columns["cust_id"],
         false);
         
    ds.Relations.Add(myRel);

    foreach (DataRow row in ds.Tables["customer"].Rows)
    {
	Console.WriteLine(row["cust_id"]);
/* Get all child rows into an array	
        DataRow[] aRows = row.GetChildRows(myRel);
	for (int childs=0;childs<aRows.Length;childs++)
	{
		Console.WriteLine("\t{0}\t{1}",
                 aRows[childs]["order_id"], 
                 aRows[childs].GetParentRow(myRel)["company"]);
	}
*/
/* or just loop the collection */
	foreach (DataRow orderRow in row.GetChildRows(myRel))
	{
		Console.WriteLine("\t{0}\t{1}",
                     orderRow["order_id"], 
                     orderRow.GetParentRow(myRel)["company"]);
	}
    }
		
/*
Add a calculated column to orders. In VFP it's like (and in VFP there are at least 3 ways):
alter table myOrders add column myContact c(50)
select Customer
scan
  update myOrders set myContact = Customer.Contact where cust_id = Customer.Cust_id
endscan
*/
   ds.Tables["orders"].Columns.Add("myContact", 
        typeof(string), 
        "Parent(CustomerOrders).Contact");

/* List orders table */
   foreach(DataRow orderRow in ds.Tables["orders"].Rows)
   {
	Console.WriteLine("OrderID:{0}\tContact:{1}",
		orderRow["order_id"],orderRow["myContact"]);
   }
  }
 }
}
As per creating a table, Create Table, Alter table etc DML commands are available through VFPOLEDB.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform