Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# BDF manipulation?
Message
From
13/12/2005 10:41:13
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Visual FoxPro and .NET
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01077139
Message ID:
01077507
Views:
57
>Thank you Cetin and Bonnie!
>
>I have been playind around with your code, and have it reading a single DBF without a problem. Would you tell me the best method of actually changing the data? For instance, using Bonnie's code, I have attemted to change the "title" in the "customers" table:
>
>
>row["title"]="Manager";
>ds.AcceptChanges();
>
>
>No errors, but no updates either.
>
>Thanks again!

Hi John,
The sample only had a select command and no update command. Here is a revised version:
using System;
using System.Data;
using System.Data.OleDb;

class test
{
  static void Main()
  {
   string strConn = @"Provider=VFPOLEDB;Data source=
C:\Program Files\Microsoft Visual FoxPro 9\Samples\Data\testdata.dbc;";

  OleDbConnection cn = new OleDbConnection(strConn);

   // Prepare command and parameters
   string strQuery = "select * from orders where cust_id = ? and order_date between ? and ?";
   string strUpdate = "update orders set to_region = ? where order_id = ?";


   OleDbCommand cmd = new OleDbCommand(strQuery,cn);
   OleDbParameter customerID = cmd.Parameters.Add("customerID",OleDbType.Char);
   OleDbParameter startDate  = cmd.Parameters.Add("startDate",OleDbType.Date);
   OleDbParameter endDate    = cmd.Parameters.Add("endDate",OleDbType.Date);

   customerID.Value = "BONAP";
   startDate.Value  = new DateTime(1995,1,1);
   endDate.Value    = new DateTime(1995,5,30);

   OleDbCommand cmdUpdate = new OleDbCommand(strUpdate,cn);
   OleDbParameter region  = cmdUpdate.Parameters.Add("region",OleDbType.Char);
   OleDbParameter orderID = cmdUpdate.Parameters.Add("orderID",OleDbType.Char);
   region.SourceColumn = "to_region";
   orderID.SourceColumn = "order_id";
   orderID.SourceVersion = DataRowVersion.Original;

  OleDbDataAdapter da = new OleDbDataAdapter();
  da.SelectCommand = cmd;
  da.UpdateCommand = cmdUpdate;

  DataSet ds = new DataSet();
  da.Fill(ds);

  // now, if you want to see your data, as Cetin showed in his example:
   foreach (DataRow row in ds.Tables[0].Rows)
   {
    Console.WriteLine("Customer ID: {0}, Order ID: {1}, Ordered on: {2}",
      (string)row["cust_id"],
      (string)row["order_id"],
      (DateTime)row["order_date"]);
   }
   DataRow row0 = ds.Tables[0].Rows[0];
   DataRow row1 = ds.Tables[0].Rows[1];
   row0["to_region"] = "HelloVFP";
   row1["to_region"] = "FromC#";
   da.Update(ds);
  }
}
Note that when using with VFP parameters are mapped to source by their ordinal positions and not names as in SQL server. IOW the first parameter added points to first ?, second to 2nd ? and so on whatever their parameter name is in your code (I might well say "myRegion" instead of "region" or "blahblah". It knows it's ? parameter in "...to_region = ? ..." just because in update command it's the first parameter and I add 'region' first to parameters collection. Hope clear).
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