tailieunhanh - Building a DataSet Programmatically

[ Team LiB ] Recipe Building a DataSet Programmatically Problem You want to build a DataSet programmatically—including adding tables, columns, primary keys, and relations—from a schema that you have designed. | Team LiB Recipe Building a DataSet Programmatically Problem You want to build a DataSet programmatically including adding tables columns primary keys and relations from a schema that you have designed. Solution The following example shows how to build a complex DataSet programmatically including how to build and add tables columns primary key constraints relations and column mappings. Use this as a template for building your own DataSet. The sample code creates a DataSet. A DataTable object is created representing the Orders table in Northwind. Columns are added including the auto-increment primary key to the table. The table is added to the DataSet. The process is repeated for a DataTable representing the Order Details table in Northwind. A DataRelation is created relating the two tables. Finally the tables are filled with data from Northwind. The C code is shown in Example 2-2. Example 2-2. File Namespaces variables and constants using System using using using . . . Create the DataSet. DataSet ds new DataSet MyDataSet Build the Orders parent table. DataTable parentTable new DataTable Orders DataColumnCollection cols Add the identity field. DataColumn column OrderID typeof true -1 -1 Add the other fields. CustomerID typeof .MaxLength 5 EmployeeID typeof OrderDate typeof RequiredDate typeof ShippedDate typeof ShipVia typeof Freight typeof ShipName typeof .MaxLength 40 ShipAddress typeof .MaxLength 60 ShipCity typeof .MaxLength 15 ShipRegion typeof .MaxLength 15 ShipPostalCode typeof .