Creating a UniqueConstraint Object

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

lượt xem

Creating a UniqueConstraint Object

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

Creating a UniqueConstraint Object In this section, you'll learn how to create a UniqueConstraint object. The UniqueConstraint constructor is overloaded as follows

Chủ đề:

Nội dung Text: Creating a UniqueConstraint Object

  1. Creating a UniqueConstraint Object In this section, you'll learn how to create a UniqueConstraint object. The UniqueConstraint 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, bool isPrimaryKey) 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 a DataTable. The following example creates and populates two DataTable objects named customersDT and ordersDT (the ordersDT object will be used later in the section "Creating 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;
  2. 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 CustomerID DataColumn of the customersDT DataTable, and then adds this UniqueConstraint to customersDT; notice that the third parameter to the UniqueConstraint constructor is set to true, 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 its properties: 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: CustomerID
  3. myUC.ConstraintName = UniqueConstraintCustomerID myUC.IsPrimaryKey = True myUC.Table = Customers The IsPrimaryKey property is true because the constraint is for a primary key; this was specified in the constructor when the constraint was created earlier.
Đồng bộ tài khoản