1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Creating a UniqueConstraint Object ppt

3 272 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Creating a UniqueConstraint Object
Định dạng
Số trang 3
Dung lượng 23,36 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

Trang 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;

Trang 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

Trang 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ày đăng: 26/01/2014, 07:20

TỪ KHÓA LIÊN QUAN

w