How do I pass one record, one row to a Method?
and
How do i access the individual fields by name after passed to the
method?
The key is to understand the OOP model of a DataSet. A DataSet includes
a number of possible objects, some of which can be DataTables. A
DataTable includes a collection of rows and columns. You often don't
need a full DataSet, especially in ADO.NET 2.0, since a single
DataTable often has the functionality you need.
A DataTable contains both DataRows and DataColumns. One oddity of
ADO.NET is that a DataRow is NOT a collection of DataColumns, and this
is where most Delphi coders have a problem at first. A DataRow can't
stand alone: it only has a meaning in the context of a DataTable.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Create a table using the DataTable(string TableName)
constructor
DataTable dt = new DataTable("Employees");
// Add 2 columns to the table
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
// Add a primary key based on a column
dt.PrimaryKey = new DataColumn[] { dt.Columns["ID"] };
// Now create some new rows with data in the new columns
DataRow dr;
dr = dt.NewRow();
dr["ID"] = 600040;
dr["Name"] = "Smith";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = 205;
dr["Name"] = "Jones";
dt.Rows.Add(dr);
// Iterate over the rows of the table - this shows how to pass
rows to another function
foreach (DataRow row in dt.Rows)
showRow(dt, row);
}
private static void showRow(DataTable dt, DataRow row)
{
// now iterate over the columns in a single row
foreach (DataColumn dc in dt.Columns)
Console.WriteLine("Column: {0}, value: {1}", dc.ColumnName,
row[dc].ToString());
Console.WriteLine(""); // blank line after each row
}
}
}