Danh mục

Creating a UniqueConstraint Object

Số trang: 3      Loại file: pdf      Dung lượng: 23.36 KB      Lượt xem: 10      Lượt tải: 0    
Hoai.2512

Phí lưu trữ: miễn phí Tải xuống file đầy đủ (3 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Creating a UniqueConstraint Object In this section, youll learn how to create a UniqueConstraint object. The UniqueConstraint constructor is overloaded as follows
Nội dung trích xuất từ tài liệu:
Creating a UniqueConstraint ObjectCreating a UniqueConstraint ObjectIn this section, youll learn how to create a UniqueConstraint object. TheUniqueConstraint constructor is overloaded as follows:UniqueConstraint(DataColumn myDataColumn)UniqueConstraint(DataColumn[] myDataColumns)UniqueConstraint(DataColumn myDataColumn, bool isPrimaryKey)UniqueConstraint(DataColumn[] myDataColumns, bool isPrimaryKey)UniqueConstraint(string constraintName, DataColumn myDataColumn)UniqueConstraint(string constraintName, DataColumn[] myDataColumns)UniqueConstraint(string constraintName, DataColumn myDataColumn, boolisPrimaryKey)UniqueConstraint(string constraintName, DataColumn[] myDataColumns, bool isPrimaryKey)UniqueConstraint(string constraintName, string[] columnNames, bool isPrimaryKey)where • myDataColumn and myDataColumns are the DataColumn objects that you want to ensure are unique. • constraintName is the name you want to assign to the ConstraintName property of your UniqueConstraint. • isPrimaryKey indicates whether your UniqueConstraint is for a primary key.Before creating and adding a UniqueConstraint to a DataTable, you first need aDataTable. The following example creates and populates two DataTable objects namedcustomersDT and ordersDT (the ordersDT object will be used later in the sectionCreating a ForeignKeyConstraint Object):SqlCommand mySqlCommand = mySqlConnection.CreateCommand();mySqlCommand.CommandText = SELECT CustomerID, CompanyName + FROM Customers + WHERE CustomerID = ALFKI + SELECT OrderID, CustomerID + FROM Orders + WHERE CustomerID = ALFKI;;SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();mySqlDataAdapter.SelectCommand = mySqlCommand;DataSet myDataSet = new DataSet();mySqlConnection.Open();mySqlDataAdapter.Fill(myDataSet);mySqlConnection.Close();myDataSet.Tables[Table].TableName = Customers;myDataSet.Tables[Table1].TableName = Orders;DataTable customersDT = myDataSet.Tables[Customers];DataTable ordersDT = myDataSet.Tables[Orders];The following example creates a UniqueConstraint object on the CustomerIDDataColumn of the customersDT DataTable, and then adds this UniqueConstraint tocustomersDT; notice that the third parameter to the UniqueConstraint constructor is set totrue, indicating that the constraint is for a primary key:UniqueConstraint myUC = new UniqueConstraint( UniqueConstraintCustomerID, customersDT.Columns[CustomerID], true );customersDT.Constraints.Add(myUC);Note To successfully add a UniqueConstraint to the DataColumn of a DataTable, the DataColumn value in each DataRow object in the DataTable must be unique.The final example retrieves the constraint just added to customersDT and displays itsproperties:myUC = (UniqueConstraint) customersDT.Constraints[UniqueConstraintCustomerID];Console.WriteLine(Columns:);foreach (DataColumn myDataColumn in myUC.Columns){ Console.WriteLine( + myDataColumn);}Console.WriteLine(myUC.ConstraintName = + myUC.ConstraintName);Console.WriteLine(myUC.IsPrimaryKey = + myUC.IsPrimaryKey);Console.WriteLine(myUC.Table = + myUC.Table);This example displays the following output:Columns: CustomerIDmyUC.ConstraintName = UniqueConstraintCustomerIDmyUC.IsPrimaryKey = TruemyUC.Table = CustomersThe IsPrimaryKey property is true because the constraint is for a primary key; this wasspecified in the constructor when the constraint was created earlier.

Tài liệu được xem nhiều: