Adding restrictions to datatable and datacolumn objects phần 2

constraintName is the name you want to assign to your constraint. isPrimaryKey indicates whether the constraint is a primary key constraint or just a regular unique constraint. | constraintName is the name you want to assign to your constraint. isPrimaryKey indicates whether the constraint is a primary key constraint or just a regular unique constraint. The following example uses the Add method to add a primary key constraint to the Products DataTable Orders . Primary key constraint Orders .Columns OrderlD true This example does the same thing as the previous example that added the primary key constraint using the PrimaryKey property. Notice the last parameter to the Add method is set to true which indicates the constraint is for a primary key. Just as an aside if you have a column that isn t a primary key but is unique you can add a UniqueConstraint object to the ConstraintsCollection. For example UniqueConstraint myUC new UniqueConstraint myColumn myU C Adding a Primary Key to the OrderDetails DataTable Let s consider an example of setting the PrimaryKey property for the Order Details DataTable. The primary for the Order Details table is made up of the OrderlD and ProductID columns and the following example sets the PrimaryKey property of the Order Details DataTable to these two columns Order Details .PrimaryKey new DataColumn Order Details .Columns OrderID Order Details .Columns ProductID The following example uses the Add method to do the same thing Order Details . Primary key constraint new DataColumn Order Details .Columns OrderID Order Details .Columns ProductID true One thing to keep in mind when adding constraints to a DataTable is that it knows only about the rows you store in it it doesn t know about any other rows stored in the actual database table. To see why this is an issue consider the following scenario that involves primary keys 1. You add a primary key constraint to a DataTable. 2. You retrieve a subset of the

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU LIÊN QUAN
5    176    1
5    255    1
5    106    0
5    121    1
6    103    1
6    107    1
6    121    1
6    103    0
6    140    0
6    98    0
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.