Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Creating a UniqueConstraint Object ppt
Nội dung xem thử
Mô tả chi tiết
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;