Adding Parent/Child Rows with Auto-Incrementing Keys

Chia sẻ: Bui Tuan | Ngày: | Loại File: PDF | Số trang:4

0
68
lượt xem
5
download

Adding Parent/Child Rows with Auto-Incrementing Keys

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

[ Team LiB ] Recipe 4.5 Adding Parent/Child Rows with Auto-Incrementing Keys Problem You want to insert related parent/child rows into a DataSet where the primary key of the parent table is an automatically incrementing value.

Chủ đề:
Lưu

Nội dung Text: Adding Parent/Child Rows with Auto-Incrementing Keys

  1. [ Team LiB ] Recipe 4.5 Adding Parent/Child Rows with Auto-Incrementing Keys Problem You want to insert related parent/child rows into a DataSet where the primary key of the parent table is an automatically incrementing value. Solution Use the DataColumn object's AutoIncrementSeed and AutoIncrementStep properties. The sample code contains two event handlers: Form.Load Sets up the example by creating a DataSet containing a parent and child table. Each table has an AutoIncrement column that is the primary key and has both a seed and step value of -1. A relation is created between the parent and child table. The default view of the parent table is bound to the data grid on the form. Add Record Button.Click Adds a new row to the parent table along with a random number of rows to the child table. The C# code is shown in Example 4-8. Example 4-8. File: AddParentChildAutoIncrementForm.cs // Namespaces, variables, and constants using System; using System.Data; using System.Data.SqlClient; // Table name constants private const String PARENTTABLENAME = "ParentTable";
  2. private const String CHILDTABLENAME = "ChildTable"; // Table column name constants for Parent table private const String PARENTID_FIELD = "ParentId"; private const String FIELD1_FIELD = "Field1"; private const String FIELD2_FIELD = "Field2"; // Table column parameter name constants for Parent table private const String CHILDID_FIELD = "ChildId"; private const String FIELD3_FIELD = "Field3"; private const String FIELD4_FIELD = "Field4"; private DataSet ds; // . . . private void AddParentChildAutoIncrementForm_Load(object sender, System.EventArgs e) { DataColumnCollection cols; DataColumn col; // Build the parent table. DataTable parentTable = new DataTable(PARENTTABLENAME); cols = parentTable.Columns; col = cols.Add(PARENTID_FIELD, typeof(Int32)); col.AutoIncrement = true; col.AutoIncrementSeed = -1; col.AutoIncrementStep = -1; cols.Add(FIELD1_FIELD, typeof(String)).MaxLength = 50; cols.Add(FIELD2_FIELD, typeof(String)).MaxLength = 50; // Build the child table. DataTable childTable = new DataTable(CHILDTABLENAME); cols = childTable.Columns; col = cols.Add(CHILDID_FIELD, typeof(Int32)); col.AutoIncrement = true; col.AutoIncrementSeed = -1; col.AutoIncrementStep = -1; cols.Add(PARENTID_FIELD, typeof(Int32)).AllowDBNull = false; cols.Add(FIELD3_FIELD, typeof(String)).MaxLength = 50; cols.Add(FIELD4_FIELD, typeof(String)).MaxLength = 50;
  3. // Add the tables to the DataSet and create the relationship. ds = new DataSet( ); ds.Tables.Add(parentTable); ds.Tables.Add(childTable); ds.Relations.Add(new DataRelation("Parent_Child_Relation", parentTable.Columns[PARENTID_FIELD], childTable.Columns[PARENTID_FIELD], true)); // Bind the parent table default view to the grid. dataGrid.DataSource = parentTable.DefaultView; } private void addRecordButton_Click(object sender, System.EventArgs e) { // Add a record to the parent table. DataRow parentRow = ds.Tables[PARENTTABLENAME].NewRow( ); parentRow[FIELD1_FIELD] = Guid.NewGuid().ToString( ); parentRow[FIELD2_FIELD] = Guid.NewGuid().ToString( ); ds.Tables[PARENTTABLENAME].Rows.Add(parentRow); // Add some child records for the parent. for(int i = 0; i
  4. [ Team LiB ]
Đồng bộ tài khoản